package com.smartleanx.module.hrm.service.programme;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.security.core.util.SecurityFrameworkUtils;
import com.smartleanx.framework.tenant.core.aop.TenantIgnore;
import com.smartleanx.module.hrm.app.empRisk.EmployeeRiskHandleChainService;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.programme.*;
import com.smartleanx.module.hrm.dal.dataobject.programme.bo.*;
import com.smartleanx.module.hrm.dal.dataobject.programme.vo.*;
import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeMapper;
import com.smartleanx.module.hrm.dal.mysql.programme.*;
import com.smartleanx.module.hrm.service.examine.util.DateUtil;
import com.smartleanx.module.hrm.service.examine.util.exception.ResultException;
import com.smartleanx.module.system.api.dept.DeptApi;
import com.smartleanx.module.system.api.dept.PostApi;
import com.smartleanx.module.system.api.dept.dto.DeptRespDTO;
import com.smartleanx.module.system.api.dept.dto.PostRespDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2024-11-28
 */
@Service
public class HrmProgrammeServiceImpl implements HrmProgrammeService {
    @Autowired
    private HrmProgrammeMapper hrmProgrammeMapper;
    @Autowired
    private HrmProgrammeService hrmProgrammeService;
    @Autowired
    private HrmProgrammeInfoMapper hrmProgrammeInfoMapper;
    @Autowired
    private HrmProgrammeInsuranceMapper hrmProgrammeInsuranceMapper;
    @Autowired
    private HrmProgrammeCompanyMapper hrmProgrammeCompanyMapper;
    @Autowired
    private HrmProgrammeRoundUpMapper hrmProgrammeRoundUpMapper;
    @Autowired
    private SystemChinaAreaMapper systemChinaAreaMapper;
    @Autowired
    private HrmProgrammeAccountMapper hrmProgrammeAccountMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private HrmProgrammeEmployeeMapper hrmProgrammeEmployeeMapper;
    @Autowired
    private HrmProgrammeEmployeeRecordMapper hrmProgrammeEmployeeRecordMapper;
    @Autowired
    private HrmProgrammeEmployeeService hrmProgrammeEmployeeService;
    @Resource
    @Lazy
    private EmployeeRiskHandleChainService employeeRiskHandleChainService;


    @Autowired
    private DeptApi deptApi;
    @Autowired
    private PostApi postApi;

    @Override
    @TenantIgnore
    public List<SystemChinaAreaVo> areaBySelect() {
        // 中国
        List<SystemChinaAreaVo> chinaAreas = systemChinaAreaMapper.selectChinaAreasByPid(0);
        chinaAreas.get(0).setFlag("china");
        return chinaAreas;

    }

    @Override
    public List<JSONObject> insuranceBySelect(String name, Integer type, Integer state) {
        List<JSONObject> resultList = new ArrayList<>();
        List<HrmProgrammeInsurance> hrmProgrammeInsurances = hrmProgrammeInsuranceMapper.selectList(new LambdaQueryWrapper<HrmProgrammeInsurance>() {{
            like(!ObjectUtils.isEmpty(name), HrmProgrammeInsurance::getName, name);
            eq(HrmProgrammeInsurance::getType, type);
            eq(!ObjectUtils.isEmpty(state), HrmProgrammeInsurance::getState, 1);
        }});
        hrmProgrammeInsurances.forEach(hrmProgrammeInsurance -> {
            JSONObject result = new JSONObject();
            result.put("id", hrmProgrammeInsurance.getId());
            result.put("name", hrmProgrammeInsurance.getName());
            resultList.add(result);
        });
        return resultList;
    }


    @Override
    public List<JSONObject> roundUpBySelect(String name) {
        List<JSONObject> resultList = new ArrayList<>();
        List<HrmProgrammeRoundUp> hrmProgrammeRoundUps = hrmProgrammeRoundUpMapper.selectList(new LambdaQueryWrapper<HrmProgrammeRoundUp>() {{
            like(!ObjectUtils.isEmpty(name), HrmProgrammeRoundUp::getName, name);
        }});
        hrmProgrammeRoundUps.forEach(hrmProgrammeRoundUp -> {
            JSONObject result = new JSONObject();
            result.put("id", hrmProgrammeRoundUp.getId());
            result.put("name", hrmProgrammeRoundUp.getName());
            resultList.add(result);
        });
        return resultList;
    }

    @Override
    public List<JSONObject> accountBySelect(String name) {
        List<JSONObject> resultList = new ArrayList<>();
        List<HrmProgrammeAccount> hrmProgrammeAccounts = hrmProgrammeAccountMapper.selectList(new LambdaQueryWrapper<HrmProgrammeAccount>() {{
            like(!ObjectUtils.isEmpty(name), HrmProgrammeAccount::getAccountName, name);
            eq(HrmProgrammeAccount::getState, 1);
        }});
        hrmProgrammeAccounts.forEach(hrmProgrammeAccount -> {
            JSONObject result = new JSONObject();
            result.put("id", hrmProgrammeAccount.getId());
            result.put("name", hrmProgrammeAccount.getAccountName());
            resultList.add(result);
        });
        return resultList;
    }

    @Override
    public Integer programmeSave(HrmProgrammeSaveBo hrmProgrammeSaveBo) {
        hrmProgrammeSaveBo.setPaymentTime("15");
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        EmployeeDO employeeDO = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
            eq(EmployeeDO::getUserId, loginUserId);
        }});
        HrmProgramme hrmProgramme = new HrmProgramme();
        hrmProgramme.setProgrammeName(hrmProgrammeSaveBo.getProgrammeName());
        hrmProgramme.setCityId(hrmProgrammeSaveBo.getCityId());
        SystemChinaArea systemChinaArea = systemChinaAreaMapper.selectById(hrmProgrammeSaveBo.getCityId());
        hrmProgramme.setCityName(systemChinaArea.getName());
        hrmProgramme.setSocialStatus(hrmProgrammeSaveBo.getSocialStatus());
        hrmProgramme.setAccumulationStatus(hrmProgrammeSaveBo.getAccumulationStatus());
        hrmProgramme.setCompanyRule(hrmProgrammeSaveBo.getCompanyRule());
        hrmProgramme.setIndividualRule(hrmProgrammeSaveBo.getIndividualRule());
        hrmProgramme.setGjjCompanyRule(hrmProgrammeSaveBo.getGjjCompanyRule());
        hrmProgramme.setGjjIndividualRule(hrmProgrammeSaveBo.getGjjIndividualRule());
        hrmProgramme.setCreateTime(DateUtil.currentDateFormat());
        if (!ObjectUtils.isEmpty(employeeDO)) {
            hrmProgramme.setCreateBy(employeeDO.getId().intValue());
        }
        hrmProgramme.setPaymentTime(hrmProgrammeSaveBo.getPaymentTime());
        hrmProgrammeMapper.insert(hrmProgramme);
        if (hrmProgrammeSaveBo.getAccumulationStatus() == 1 && hrmProgrammeSaveBo.getSocialStatus() == 1) {
            List<HrmProgrammeInfoSaveBo> programmeSbInfos = hrmProgrammeSaveBo.getProgrammeSbInfos();
            programmeSbInfos.forEach(programmeInfoSaveBo -> {
                HrmProgrammeInfo programmeInfo = new HrmProgrammeInfo();
                programmeInfo.setProgrammeId(hrmProgramme.getId());
                programmeInfo.setInsuranceId(programmeInfoSaveBo.getInsuranceId());
                programmeInfo.setCardinalityLowerLimit(programmeInfoSaveBo.getCardinalityLowerLimit());
                programmeInfo.setBaseUpperLimit(programmeInfoSaveBo.getBaseUpperLimit());
                programmeInfo.setCompanyProportion(programmeInfoSaveBo.getCompanyProportion());
                programmeInfo.setIndividualProportion(programmeInfoSaveBo.getIndividualProportion());
                programmeInfo.setCompanyRule(programmeInfoSaveBo.getCompanyRule());
                programmeInfo.setIndividualRule(programmeInfoSaveBo.getIndividualRule());
                hrmProgrammeInfoMapper.insert(programmeInfo);
            });
            List<HrmProgrammeInfoSaveBo> programmeGjjInfos = hrmProgrammeSaveBo.getProgrammeGjjInfos();
            programmeGjjInfos.forEach(programmeInfoSaveBo -> {
                HrmProgrammeInfo programmeInfo = new HrmProgrammeInfo();
                programmeInfo.setProgrammeId(hrmProgramme.getId());
                programmeInfo.setInsuranceId(programmeInfoSaveBo.getInsuranceId());
                programmeInfo.setCardinalityLowerLimit(programmeInfoSaveBo.getCardinalityLowerLimit());
                programmeInfo.setBaseUpperLimit(programmeInfoSaveBo.getBaseUpperLimit());
                programmeInfo.setCompanyProportion(programmeInfoSaveBo.getCompanyProportion());
                programmeInfo.setIndividualProportion(programmeInfoSaveBo.getIndividualProportion());
                programmeInfo.setCompanyRule(programmeInfoSaveBo.getCompanyRule());
                programmeInfo.setIndividualRule(programmeInfoSaveBo.getIndividualRule());
                hrmProgrammeInfoMapper.insert(programmeInfo);
            });
        } else if (hrmProgrammeSaveBo.getAccumulationStatus() == 1 && hrmProgrammeSaveBo.getSocialStatus() == 0) {
            List<HrmProgrammeInfoSaveBo> programmeGjjInfos = hrmProgrammeSaveBo.getProgrammeGjjInfos();
            programmeGjjInfos.forEach(programmeInfoSaveBo -> {
                HrmProgrammeInfo programmeInfo = new HrmProgrammeInfo();
                programmeInfo.setProgrammeId(hrmProgramme.getId());
                programmeInfo.setInsuranceId(programmeInfoSaveBo.getInsuranceId());
                programmeInfo.setCardinalityLowerLimit(programmeInfoSaveBo.getCardinalityLowerLimit());
                programmeInfo.setBaseUpperLimit(programmeInfoSaveBo.getBaseUpperLimit());
                programmeInfo.setCompanyProportion(programmeInfoSaveBo.getCompanyProportion());
                programmeInfo.setIndividualProportion(programmeInfoSaveBo.getIndividualProportion());
                programmeInfo.setCompanyRule(programmeInfoSaveBo.getCompanyRule());
                programmeInfo.setIndividualRule(programmeInfoSaveBo.getIndividualRule());
                hrmProgrammeInfoMapper.insert(programmeInfo);
            });
        } else if (hrmProgrammeSaveBo.getAccumulationStatus() == 0 && hrmProgrammeSaveBo.getSocialStatus() == 1) {
            List<HrmProgrammeInfoSaveBo> programmeSbInfos = hrmProgrammeSaveBo.getProgrammeSbInfos();
            programmeSbInfos.forEach(programmeInfoSaveBo -> {
                HrmProgrammeInfo programmeInfo = new HrmProgrammeInfo();
                programmeInfo.setProgrammeId(hrmProgramme.getId());
                programmeInfo.setInsuranceId(programmeInfoSaveBo.getInsuranceId());
                programmeInfo.setCardinalityLowerLimit(programmeInfoSaveBo.getCardinalityLowerLimit());
                programmeInfo.setBaseUpperLimit(programmeInfoSaveBo.getBaseUpperLimit());
                programmeInfo.setCompanyProportion(programmeInfoSaveBo.getCompanyProportion());
                programmeInfo.setIndividualProportion(programmeInfoSaveBo.getIndividualProportion());
                programmeInfo.setCompanyRule(programmeInfoSaveBo.getCompanyRule());
                programmeInfo.setIndividualRule(programmeInfoSaveBo.getIndividualRule());
                hrmProgrammeInfoMapper.insert(programmeInfo);
            });
        }
        return 1;
    }

    @Override
    public HrmProgrammeUpdateVo selectInsertHX() {
        List<HrmProgrammeInfoUpdateVo> programmeGjjInfos = new ArrayList<>();
        List<HrmProgrammeInfoUpdateVo> programmeSbInfos = new ArrayList<>();
        List<HrmProgrammeInsurance> programmeGjj = hrmProgrammeInsuranceMapper.selectList(new LambdaQueryWrapper<HrmProgrammeInsurance>() {{
            eq(HrmProgrammeInsurance::getType, 2);
            eq(HrmProgrammeInsurance::getState, 1);
        }});
        List<HrmProgrammeInsurance> programmeSb = hrmProgrammeInsuranceMapper.selectList(new LambdaQueryWrapper<HrmProgrammeInsurance>() {{
            eq(HrmProgrammeInsurance::getType, 1);
            eq(HrmProgrammeInsurance::getState, 1);
        }});
        HrmProgrammeUpdateVo hrmProgrammeUpdateVo = new HrmProgrammeUpdateVo();
        hrmProgrammeUpdateVo.setSocialStatus(1);
        hrmProgrammeUpdateVo.setAccumulationStatus(1);
        programmeSb.forEach(programmeInfoSaveBo -> {
            HrmProgrammeInfoUpdateVo programmeInfoUpdateVo = new HrmProgrammeInfoUpdateVo();
            programmeInfoUpdateVo.setInsuranceId(programmeInfoSaveBo.getId());
            programmeInfoUpdateVo.setIndividualRule(3);
            programmeInfoUpdateVo.setCompanyRule(3);
            programmeSbInfos.add(programmeInfoUpdateVo);
        });
        programmeGjj.forEach(programmeInfoSaveBo -> {
            HrmProgrammeInfoUpdateVo programmeInfoUpdateVo = new HrmProgrammeInfoUpdateVo();
            programmeInfoUpdateVo.setInsuranceId(programmeInfoSaveBo.getId());

            programmeInfoUpdateVo.setIndividualRule(3);
            programmeInfoUpdateVo.setCompanyRule(3);
            programmeGjjInfos.add(programmeInfoUpdateVo);
        });
        hrmProgrammeUpdateVo.setProgrammeSbInfos(programmeSbInfos);
        hrmProgrammeUpdateVo.setProgrammeGjjInfos(programmeGjjInfos);
        hrmProgrammeUpdateVo.setCompanyRule(3);
        hrmProgrammeUpdateVo.setIndividualRule(3);
        hrmProgrammeUpdateVo.setGjjIndividualRule(3);
        hrmProgrammeUpdateVo.setGjjCompanyRule(3);
        return hrmProgrammeUpdateVo;
    }

    @Override
    public PageInfo<HrmProgramme> selectProgramme(Integer cityId, String programmeName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<HrmProgramme> hrmProgrammes = hrmProgrammeMapper.selectList(new LambdaQueryWrapper<HrmProgramme>() {{
            eq(!ObjectUtils.isEmpty(programmeName), HrmProgramme::getProgrammeName, programmeName);
            eq(!ObjectUtils.isEmpty(cityId), HrmProgramme::getCityId, cityId);
//            eq(HrmProgramme::getState, 1);
            orderByDesc(HrmProgramme::getId);
        }});
        return new PageInfo<>(hrmProgrammes);
    }




    @Override
    public Integer removeProgramme(Integer id) {
        Long l = hrmProgrammeEmployeeMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getProgrammeId, id);
        }});
        if(l>0){
            throw new ResultException("缴纳方案正在使用不可以删除");
        }else {
            hrmProgrammeMapper.deleteById(id);
            hrmProgrammeInfoMapper.delete(new LambdaQueryWrapper<HrmProgrammeInfo>(){{
                eq(HrmProgrammeInfo::getProgrammeId, id);
            }});
        }
        return 0;
    }

    @Override
    public Integer updateState(Integer id, Integer state) {
        HrmProgramme hrmProgramme = hrmProgrammeMapper.selectById(id);
        hrmProgramme.setState(state);
        hrmProgrammeMapper.updateById(hrmProgramme);
        return 0;
    }

    @Override
    public List<JSONObject> programmeBySelect(Integer cityId) {
        List<JSONObject> resultList = new ArrayList<>();
        List<HrmProgramme> hrmProgrammes = hrmProgrammeMapper.selectList(new LambdaQueryWrapper<HrmProgramme>().eq(HrmProgramme::getCityId,cityId).eq(HrmProgramme::getState,1));
        hrmProgrammes.forEach(hrmProgramme -> {
            JSONObject result = new JSONObject();
            result.put("id", hrmProgramme.getId());
            result.put("name", hrmProgramme.getProgrammeName());
            resultList.add(result);
        });

        return resultList;
    }


    @Override
    public HrmProgrammeUpdateVo selectProgrammeHX(Integer ProgrammeId) {
        HrmProgramme hrmProgramme = hrmProgrammeMapper.selectById(ProgrammeId);
        List<HrmProgrammeInfoUpdateVo> programmeGjjInfos = new ArrayList<>();
        List<HrmProgrammeInfoUpdateVo> programmeSbInfos = new ArrayList<>();
        List<HrmProgrammeInfo> hrmProgrammeInfos = hrmProgrammeInfoMapper.selectList(new LambdaQueryWrapper<HrmProgrammeInfo>() {{
            eq(HrmProgrammeInfo::getProgrammeId, hrmProgramme.getId());
        }});
        hrmProgrammeInfos.forEach(hrmProgrammeInfo -> {
            Integer insuranceId = hrmProgrammeInfo.getInsuranceId();
            HrmProgrammeInsurance hrmProgrammeInsurance = hrmProgrammeInsuranceMapper.selectById(insuranceId);
            if (hrmProgrammeInsurance.getType() == 1) {
                HrmProgrammeInfoUpdateVo hrmProgrammeInfoUpdateVo = new HrmProgrammeInfoUpdateVo();
                hrmProgrammeInfoUpdateVo.setInsuranceId(hrmProgrammeInfo.getId());
                hrmProgrammeInfoUpdateVo.setIndividualProportion(hrmProgrammeInfo.getIndividualProportion());
                hrmProgrammeInfoUpdateVo.setCompanyProportion(hrmProgrammeInfo.getCompanyProportion());
                hrmProgrammeInfoUpdateVo.setIndividualRule(hrmProgrammeInfo.getIndividualRule());
                hrmProgrammeInfoUpdateVo.setCompanyRule(hrmProgrammeInfo.getCompanyRule());
                hrmProgrammeInfoUpdateVo.setInsuranceId(insuranceId);
                hrmProgrammeInfoUpdateVo.setCardinalityLowerLimit(hrmProgrammeInfo.getCardinalityLowerLimit());
                hrmProgrammeInfoUpdateVo.setBaseUpperLimit(hrmProgrammeInfo.getBaseUpperLimit());
                programmeSbInfos.add(hrmProgrammeInfoUpdateVo);
            } else {
                HrmProgrammeInfoUpdateVo hrmProgrammeInfoUpdateVo = new HrmProgrammeInfoUpdateVo();
                hrmProgrammeInfoUpdateVo.setInsuranceId(hrmProgrammeInfo.getId());
                hrmProgrammeInfoUpdateVo.setIndividualProportion(hrmProgrammeInfo.getIndividualProportion());
                hrmProgrammeInfoUpdateVo.setCompanyProportion(hrmProgrammeInfo.getCompanyProportion());
                hrmProgrammeInfoUpdateVo.setIndividualRule(hrmProgrammeInfo.getIndividualRule());
                hrmProgrammeInfoUpdateVo.setCompanyRule(hrmProgrammeInfo.getCompanyRule());
                hrmProgrammeInfoUpdateVo.setInsuranceId(insuranceId);
                hrmProgrammeInfoUpdateVo.setCardinalityLowerLimit(hrmProgrammeInfo.getCardinalityLowerLimit());
                hrmProgrammeInfoUpdateVo.setBaseUpperLimit(hrmProgrammeInfo.getBaseUpperLimit());
                programmeGjjInfos.add(hrmProgrammeInfoUpdateVo);
            }
        });
        HrmProgrammeUpdateVo hrmProgrammeUpdateVo = new HrmProgrammeUpdateVo();
        hrmProgrammeUpdateVo.setSocialStatus(hrmProgramme.getSocialStatus());
        hrmProgrammeUpdateVo.setAccumulationStatus(hrmProgramme.getAccumulationStatus());
        hrmProgrammeUpdateVo.setCityId(hrmProgramme.getCityId());
        hrmProgrammeUpdateVo.setProgrammeName(hrmProgramme.getProgrammeName());
        hrmProgrammeUpdateVo.setCompanyRule(hrmProgramme.getCompanyRule());
        hrmProgrammeUpdateVo.setIndividualRule(hrmProgramme.getIndividualRule());
        hrmProgrammeUpdateVo.setCreateTime(hrmProgramme.getCreateTime());
        hrmProgrammeUpdateVo.setGjjCompanyRule(hrmProgramme.getGjjCompanyRule());
        hrmProgrammeUpdateVo.setGjjIndividualRule(hrmProgramme.getGjjIndividualRule());
        if (hrmProgramme.getCreateBy() == null) {
            hrmProgrammeUpdateVo.setCreateBy("管理员");
        } else {
            employeeMapper.selectById(hrmProgramme.getCreateBy());
        }

        hrmProgrammeUpdateVo.setPaymentTime(hrmProgramme.getPaymentTime());
        hrmProgrammeUpdateVo.setProgrammeSbInfos(programmeSbInfos);
        hrmProgrammeUpdateVo.setProgrammeGjjInfos(programmeGjjInfos);
        return hrmProgrammeUpdateVo;
    }

    @Override
    public Integer programmeUpdate(HrmProgrammeSaveBo hrmProgrammeSaveBo) {
        hrmProgrammeSaveBo.setPaymentTime("15");
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        EmployeeDO employeeDO = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
            eq(EmployeeDO::getUserId, loginUserId);
        }});
        hrmProgrammeInfoMapper.delete(new LambdaQueryWrapper<HrmProgrammeInfo>() {{
            eq(HrmProgrammeInfo::getProgrammeId, hrmProgrammeSaveBo.getId());
        }});
        HrmProgramme hrmProgramme = new HrmProgramme();
        hrmProgramme.setId(hrmProgrammeSaveBo.getId());
        hrmProgramme.setProgrammeName(hrmProgrammeSaveBo.getProgrammeName());
        hrmProgramme.setCityId(hrmProgrammeSaveBo.getCityId());
        SystemChinaArea systemChinaArea = systemChinaAreaMapper.selectById(hrmProgrammeSaveBo.getCityId());
        hrmProgramme.setCityName(systemChinaArea.getName());
        hrmProgramme.setSocialStatus(hrmProgrammeSaveBo.getSocialStatus());
        hrmProgramme.setAccumulationStatus(hrmProgrammeSaveBo.getAccumulationStatus());
        hrmProgramme.setCompanyRule(hrmProgrammeSaveBo.getCompanyRule());
        hrmProgramme.setIndividualRule(hrmProgrammeSaveBo.getIndividualRule());
        hrmProgramme.setCreateTime(DateUtil.currentDateFormat());
        hrmProgramme.setGjjIndividualRule(hrmProgrammeSaveBo.getGjjIndividualRule());
        hrmProgramme.setGjjCompanyRule(hrmProgrammeSaveBo.getGjjCompanyRule());

        if (!ObjectUtils.isEmpty(employeeDO)) {
            hrmProgramme.setCreateBy(employeeDO.getId().intValue());
        }

        hrmProgramme.setPaymentTime(hrmProgrammeSaveBo.getPaymentTime());
        hrmProgrammeMapper.updateById(hrmProgramme);

        if (hrmProgrammeSaveBo.getAccumulationStatus() == 1 && hrmProgrammeSaveBo.getSocialStatus() == 1) {
            List<HrmProgrammeInfoSaveBo> programmeSbInfos = hrmProgrammeSaveBo.getProgrammeSbInfos();
            programmeSbInfos.forEach(programmeInfoSaveBo -> {
                HrmProgrammeInfo programmeInfo = new HrmProgrammeInfo();
                programmeInfo.setProgrammeId(hrmProgramme.getId());
                programmeInfo.setInsuranceId(programmeInfoSaveBo.getInsuranceId());
                programmeInfo.setCardinalityLowerLimit(programmeInfoSaveBo.getCardinalityLowerLimit());
                programmeInfo.setBaseUpperLimit(programmeInfoSaveBo.getBaseUpperLimit());
                programmeInfo.setCompanyProportion(programmeInfoSaveBo.getCompanyProportion());
                programmeInfo.setIndividualProportion(programmeInfoSaveBo.getIndividualProportion());
                programmeInfo.setCompanyRule(programmeInfoSaveBo.getCompanyRule());
                programmeInfo.setIndividualRule(programmeInfoSaveBo.getIndividualRule());
                hrmProgrammeInfoMapper.insert(programmeInfo);
            });
            List<HrmProgrammeInfoSaveBo> programmeGjjInfos = hrmProgrammeSaveBo.getProgrammeGjjInfos();
            programmeGjjInfos.forEach(programmeInfoSaveBo -> {
                HrmProgrammeInfo programmeInfo = new HrmProgrammeInfo();
                programmeInfo.setProgrammeId(hrmProgramme.getId());
                programmeInfo.setInsuranceId(programmeInfoSaveBo.getInsuranceId());
                programmeInfo.setCardinalityLowerLimit(programmeInfoSaveBo.getCardinalityLowerLimit());
                programmeInfo.setBaseUpperLimit(programmeInfoSaveBo.getBaseUpperLimit());
                programmeInfo.setCompanyProportion(programmeInfoSaveBo.getCompanyProportion());
                programmeInfo.setIndividualProportion(programmeInfoSaveBo.getIndividualProportion());
                programmeInfo.setCompanyRule(programmeInfoSaveBo.getCompanyRule());
                programmeInfo.setIndividualRule(programmeInfoSaveBo.getIndividualRule());
                hrmProgrammeInfoMapper.insert(programmeInfo);
            });
        } else if (hrmProgrammeSaveBo.getAccumulationStatus() == 1 && hrmProgrammeSaveBo.getSocialStatus() == 0) {
            List<HrmProgrammeInfoSaveBo> programmeGjjInfos = hrmProgrammeSaveBo.getProgrammeGjjInfos();
            programmeGjjInfos.forEach(programmeInfoSaveBo -> {
                HrmProgrammeInfo programmeInfo = new HrmProgrammeInfo();
                programmeInfo.setProgrammeId(hrmProgramme.getId());
                programmeInfo.setInsuranceId(programmeInfoSaveBo.getInsuranceId());
                programmeInfo.setCardinalityLowerLimit(programmeInfoSaveBo.getCardinalityLowerLimit());
                programmeInfo.setBaseUpperLimit(programmeInfoSaveBo.getBaseUpperLimit());
                programmeInfo.setCompanyProportion(programmeInfoSaveBo.getCompanyProportion());
                programmeInfo.setIndividualProportion(programmeInfoSaveBo.getIndividualProportion());
                programmeInfo.setCompanyRule(programmeInfoSaveBo.getCompanyRule());
                programmeInfo.setIndividualRule(programmeInfoSaveBo.getIndividualRule());
                hrmProgrammeInfoMapper.insert(programmeInfo);
            });
        } else if (hrmProgrammeSaveBo.getAccumulationStatus() == 0 && hrmProgrammeSaveBo.getSocialStatus() == 1) {
            List<HrmProgrammeInfoSaveBo> programmeSbInfos = hrmProgrammeSaveBo.getProgrammeSbInfos();
            programmeSbInfos.forEach(programmeInfoSaveBo -> {
                HrmProgrammeInfo programmeInfo = new HrmProgrammeInfo();
                programmeInfo.setProgrammeId(hrmProgramme.getId());
                programmeInfo.setInsuranceId(programmeInfoSaveBo.getInsuranceId());
                programmeInfo.setCardinalityLowerLimit(programmeInfoSaveBo.getCardinalityLowerLimit());
                programmeInfo.setBaseUpperLimit(programmeInfoSaveBo.getBaseUpperLimit());
                programmeInfo.setCompanyProportion(programmeInfoSaveBo.getCompanyProportion());
                programmeInfo.setIndividualProportion(programmeInfoSaveBo.getIndividualProportion());
                programmeInfo.setCompanyRule(programmeInfoSaveBo.getCompanyRule());
                programmeInfo.setIndividualRule(programmeInfoSaveBo.getIndividualRule());
                hrmProgrammeInfoMapper.insert(programmeInfo);
            });
        }
        //修改员工参保记录
        List<HrmProgrammeEmployee> list = hrmProgrammeEmployeeMapper.selectlistByProgrammeId(hrmProgrammeSaveBo.getId());
        List<HrmProgrammeEmployee> result = updateEmployeeProgramme(list, hrmProgrammeSaveBo.getProgrammeSbInfos(), hrmProgrammeSaveBo.getProgrammeGjjInfos());
        hrmProgrammeEmployeeService.saveOrUpdateBatch(result);
        return 1;
    }

    public List<HrmProgrammeEmployee> updateEmployeeProgramme(List<HrmProgrammeEmployee> list,List<HrmProgrammeInfoSaveBo> programmeSbInfos, List<HrmProgrammeInfoSaveBo> programmeGjjInfos){
        //社保项 以险种id 转map
        Map<Integer, HrmProgrammeInfoSaveBo> subInfoMap = programmeSbInfos.stream().collect(Collectors.toMap(HrmProgrammeInfoSaveBo::getInsuranceId, Function.identity()));
        Map<Integer, HrmProgrammeInfoSaveBo> gjjMap = programmeGjjInfos.stream().collect(Collectors.toMap(HrmProgrammeInfoSaveBo::getInsuranceId, Function.identity()));
        //1. 获取需要修改或者添加的员工方案
        list.stream()
                //1.1 过滤已经停缴的信息
                .filter(p ->p.getProgrammeState() !=2&&p.getProgrammeState() !=0)
                //1.2 修改社保公积金基数
                .forEach(p ->{
                    //1.3 社保基数 转为json数组
                    cn.hutool.json.JSONArray result = new cn.hutool.json.JSONArray();
                    cn.hutool.json.JSONArray programmeLimitList = new cn.hutool.json.JSONArray(p.getProgrammeLimitStr());
                    for (Object o : programmeLimitList) {
                        cn.hutool.json.JSONObject object = new cn.hutool.json.JSONObject(o);
                        Integer insuranceId = object.getInt("insuranceId");
                        //1.3.0 覆盖时间  只有id是null时  做新增覆盖时间
                        if(null == p.getId()){
                            p.setProgrammeStartTime(DateUtil.currentYearAndMonth());
                        }

                        //1.3.1 匹配方案社保基数
                        HrmProgrammeInfoSaveBo hrmProgrammeInfoSaveBo = subInfoMap.get(insuranceId);
                        if(null == hrmProgrammeInfoSaveBo){
                            continue;
                        }
                        //1.3.2 获取个人险种缴纳金额
                        BigDecimal amount = object.getBigDecimal("amount");
                        //1.3.3 比较缴纳金额  修改方案最小基数  大于原缴纳金额基数  修改
                        if(hrmProgrammeInfoSaveBo.getCardinalityLowerLimit().compareTo(amount) > 0){
                            object.set("amount",hrmProgrammeInfoSaveBo.getCardinalityLowerLimit());
                        }
                        result.put(object);
                    }
                    p.setProgrammeLimitStr(result.toString());
                    //1.4 公积金
                    if(p.getIsAccumulation() == 1){
                        cn.hutool.json.JSONArray accumulationResult = new cn.hutool.json.JSONArray();
                        cn.hutool.json.JSONArray accumulationLimitList = new cn.hutool.json.JSONArray(p.getAccumulationLimitStr());
                        for (Object o : accumulationLimitList) {
                            cn.hutool.json.JSONObject object = new cn.hutool.json.JSONObject(o);
                            Integer insuranceId = object.getInt("insuranceId");
                            //1.4.0 判断时间 公积金缴纳时间  小于当前时间  覆盖
                            DateTime accumulationStartTime = cn.hutool.core.date.DateUtil.parse(p.getAccumulationStartTime(), DatePattern.NORM_MONTH_PATTERN);
                            DateTime currentYearAndMonth = cn.hutool.core.date.DateUtil.parse(DateUtil.currentYearAndMonth(), DatePattern.NORM_MONTH_PATTERN);
                            if(cn.hutool.core.date.DateUtil.compare(currentYearAndMonth, accumulationStartTime) > 0){
                                p.setAccumulationStartTime(DateUtil.currentYearAndMonth());
                            }
                            //1.4.1 匹配方案公积金基数
                            HrmProgrammeInfoSaveBo hrmProgrammeInfoSaveBo = gjjMap.get(insuranceId);
                            if(null == hrmProgrammeInfoSaveBo){
                                continue;
                            }
                            //1.4.2 获取个人险种缴纳金额
                            BigDecimal amount = object.getBigDecimal("amount");
                            //1.4.3 比较缴纳金额  修改方案最小基数  大于原缴纳金额基数  修改
                            if(hrmProgrammeInfoSaveBo.getCardinalityLowerLimit().compareTo(amount) > 0){
                                object.set("amount",hrmProgrammeInfoSaveBo.getCardinalityLowerLimit());
                            }
                            accumulationResult.put(object);

                        }
                        p.setAccumulationLimitStr(accumulationResult.toString());
                    }
                });

        return list;

    }

    @Override
    public PageInfo<ProgrammeEmployeeVo> selectProgrammeEmployeeBySelect(ProgrammeEmployeeBo programmeEmployeeBo) {
        //当前年+月
        String currentYearAndMonth = DateUtil.currentYearAndMonth();
        PageHelper.startPage(programmeEmployeeBo.getPageNum(), programmeEmployeeBo.getPageSize());
        List<ProgrammeEmployeeVo> programmeEmployeeVos = hrmProgrammeMapper.selectProgrammeEmployeeVoList(programmeEmployeeBo);
        programmeEmployeeVos.forEach(programmeEmployeeVo -> {
            CommonResult<DeptRespDTO> dept = deptApi.getDept(programmeEmployeeVo.getDeptId().longValue());
            if (!ObjectUtils.isEmpty(dept.getData())) {
                programmeEmployeeVo.setDeptName(dept.getCheckedData().getName());
            }
            CommonResult<PostRespDTO> post = postApi.getPost(programmeEmployeeVo.getPostId());
            if (!ObjectUtils.isEmpty(post.getData())) {
                programmeEmployeeVo.setPostName( post.getCheckedData().getName());
            }
            //是否有这个员工的参保或公积金记录
            Long isRecord = hrmProgrammeEmployeeMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
                eq(HrmProgrammeEmployee::getUserId, programmeEmployeeVo.getEmployeeId());
            }});
            if (isRecord > 0) {
                programmeEmployeeVo.setStatus(1);
                //查看当前是否参保记录
                HrmProgrammeEmployee isProgrammeRecordHistory = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
                    eq(HrmProgrammeEmployee::getUserId, programmeEmployeeVo.getEmployeeId());
                    le(HrmProgrammeEmployee::getProgrammeStartTime, currentYearAndMonth);
                    eq(HrmProgrammeEmployee::getProgrammeState, 1);
                    eq(HrmProgrammeEmployee::getIsProgramme, 1);
//                    orderByDesc(HrmProgrammeEmployee::getProgrammeStartTime);
                    last("limit 1");
                }});
                //没有历史则查状态是否未来参保
                if (ObjectUtils.isEmpty(isProgrammeRecordHistory)) {
                    //查看是否有新增未来参保记录
                    HrmProgrammeEmployee isProgrammeRecordFuture = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
                        eq(HrmProgrammeEmployee::getUserId, programmeEmployeeVo.getEmployeeId());
                        gt(HrmProgrammeEmployee::getProgrammeStartTime, currentYearAndMonth);
                        eq(HrmProgrammeEmployee::getProgrammeState, 1);
                        eq(HrmProgrammeEmployee::getIsProgramme, 1);
//                        orderByDesc(HrmProgrammeEmployee::getProgrammeStartTime);
                        last("limit 1");
                    }});
                    if (ObjectUtils.isEmpty(isProgrammeRecordFuture)) {
                        programmeEmployeeVo.setProgrammeStatus(-1);
                    } else {
                        programmeEmployeeVo.setPStartTime(isProgrammeRecordFuture.getProgrammeStartTime());
                        programmeEmployeeVo.setProgrammeStatus(1);
                    }
                } else {
                    programmeEmployeeVo.setPStartTime(isProgrammeRecordHistory.getProgrammeStartTime());
                    programmeEmployeeVo.setProgrammeStatus(0);
                }
                HrmProgrammeEmployee isProgrammeRecordStop = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
                    eq(HrmProgrammeEmployee::getUserId, programmeEmployeeVo.getEmployeeId());
                    le(HrmProgrammeEmployee::getProgrammeStartTime, DateUtil.currentYearAndMonth());
                    eq(HrmProgrammeEmployee::getProgrammeState, 2);
                    eq(HrmProgrammeEmployee::getIsProgramme, 1);
//                    orderByDesc(HrmProgrammeEmployee::getProgrammeStartTime);
                    last("limit 1");
                }});
                if (!ObjectUtils.isEmpty(isProgrammeRecordStop)) {
                    //查看是否有新增未来参保记录
                    HrmProgrammeEmployee isProgrammeRecordFuture = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
                        eq(HrmProgrammeEmployee::getUserId, programmeEmployeeVo.getEmployeeId());
                        gt(HrmProgrammeEmployee::getProgrammeStartTime, currentYearAndMonth);
                        eq(HrmProgrammeEmployee::getProgrammeState, 1);
                        eq(HrmProgrammeEmployee::getIsProgramme, 1);
//                        orderByDesc(HrmProgrammeEmployee::getProgrammeStartTime);
                        last("limit 1");
                    }});
                    if (!ObjectUtils.isEmpty(isProgrammeRecordFuture)) {
                        programmeEmployeeVo.setPStartTime(isProgrammeRecordFuture.getProgrammeStartTime());
                        programmeEmployeeVo.setProgrammeStatus(1);
                    } else {
                        programmeEmployeeVo.setPStartTime("--");
                        programmeEmployeeVo.setProgrammeStatus(2);
                    }
                }
                //查看当前是否缴纳公积金
                HrmProgrammeEmployee isAccumulationRecordHistory = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
                    eq(HrmProgrammeEmployee::getUserId, programmeEmployeeVo.getEmployeeId());
                    le(HrmProgrammeEmployee::getAccumulationStartTime, currentYearAndMonth);
                    eq(HrmProgrammeEmployee::getIsAccumulation, 1);
                    eq(HrmProgrammeEmployee::getProgrammeState, 1);
//                    orderByDesc(HrmProgrammeEmployee::getAccumulationStartTime);
                    last("limit 1");
                }});
                if (ObjectUtils.isEmpty(isAccumulationRecordHistory)) {
                    //查看是否有新增未来公积金记录
                    HrmProgrammeEmployee isAccumulationRecordFuture = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
                        eq(HrmProgrammeEmployee::getUserId, programmeEmployeeVo.getEmployeeId());
                        gt(HrmProgrammeEmployee::getAccumulationStartTime, currentYearAndMonth);
                        eq(HrmProgrammeEmployee::getIsAccumulation, 1);
                        eq(HrmProgrammeEmployee::getProgrammeState, 1);
//                        orderByDesc(HrmProgrammeEmployee::getProgrammeStartTime);
                        last("limit 1");
                    }});
                    if (ObjectUtils.isEmpty(isAccumulationRecordFuture)) {
                        programmeEmployeeVo.setStatus(2);
                        programmeEmployeeVo.setAccumulationStatus(-1);
                    } else {
                        programmeEmployeeVo.setAStartTime(isAccumulationRecordFuture.getAccumulationStartTime());
                        programmeEmployeeVo.setAccumulationStatus(1);
                    }
                } else {
                    programmeEmployeeVo.setAStartTime(isAccumulationRecordHistory.getAccumulationStartTime());

                    programmeEmployeeVo.setAccumulationStatus(0);
                }
                HrmProgrammeEmployee isAccumulationRecordStop = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
                    eq(HrmProgrammeEmployee::getUserId, programmeEmployeeVo.getEmployeeId());
                    le(HrmProgrammeEmployee::getAccumulationStartTime, DateUtil.currentYearAndMonth());
                    eq(HrmProgrammeEmployee::getProgrammeState, 2);
                    eq(HrmProgrammeEmployee::getIsAccumulation, 1);
//                    orderByDesc(HrmProgrammeEmployee::getAccumulationStartTime);
                    last("limit 1");
                }});
                if (!ObjectUtils.isEmpty(isAccumulationRecordStop)) {
                    //查看是否有新增未来参保记录
                    HrmProgrammeEmployee isProgrammeRecordFuture = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
                        eq(HrmProgrammeEmployee::getUserId, programmeEmployeeVo.getEmployeeId());
                        gt(HrmProgrammeEmployee::getProgrammeStartTime, currentYearAndMonth);
                        eq(HrmProgrammeEmployee::getProgrammeState, 1);
                        eq(HrmProgrammeEmployee::getIsAccumulation, 1);
//                        orderByDesc(HrmProgrammeEmployee::getProgrammeStartTime);
                        last("limit 1");
                    }});
                    if (!ObjectUtils.isEmpty(isProgrammeRecordFuture)) {
                        programmeEmployeeVo.setAStartTime(isProgrammeRecordFuture.getProgrammeStartTime());
                        programmeEmployeeVo.setAccumulationStatus(1);
                    } else {
                        programmeEmployeeVo.setAStartTime("--");
                        programmeEmployeeVo.setStatus(2);
                        programmeEmployeeVo.setAccumulationStatus(2);
                    }
                }
            } else {

                programmeEmployeeVo.setStatus(-1);
                programmeEmployeeVo.setAccumulationStatus(-1);
                programmeEmployeeVo.setProgrammeStatus(-1);
            }
        });
        if (!ObjectUtils.isEmpty(programmeEmployeeBo.getProgrammeStatus())) {
            programmeEmployeeVos = programmeEmployeeVos.stream().filter(programmeEmployeeVo -> programmeEmployeeVo.getProgrammeStatus() == programmeEmployeeBo.getProgrammeStatus()).collect(Collectors.toList());
        }
        if (!ObjectUtils.isEmpty(programmeEmployeeBo.getAccumulationStatus())) {
            programmeEmployeeVos = programmeEmployeeVos.stream().filter(programmeEmployeeVo -> programmeEmployeeVo.getAccumulationStatus() == programmeEmployeeBo.getAccumulationStatus()).collect(Collectors.toList());
        }

        return new PageInfo<>(programmeEmployeeVos);
    }

    @Override
    public Integer insertProgrammeEmployee(ProgrammeEmployeeSaveBo programmeEmployeeSaveBo) {
        Long programmeCount = hrmProgrammeEmployeeMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getUserId, programmeEmployeeSaveBo.getEmployeeId());
            eq(HrmProgrammeEmployee::getCompanyId, programmeEmployeeSaveBo.getCompanyId());
            eq(HrmProgrammeEmployee::getProgrammeStartTime, programmeEmployeeSaveBo.getProgrammeStartTime());
            eq(HrmProgrammeEmployee::getIsProgramme, 1);
            eq(HrmProgrammeEmployee::getProgrammeState, 1);
        }});
        if (programmeCount > 0) {
            throw new ResultException("社保已存在相同的缴纳模版");
        }
        Long accumulationCount = hrmProgrammeEmployeeMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getUserId, programmeEmployeeSaveBo.getEmployeeId());
            eq(HrmProgrammeEmployee::getCompanyId, programmeEmployeeSaveBo.getCompanyId());
            eq(HrmProgrammeEmployee::getAccumulationStartTime, programmeEmployeeSaveBo.getProgrammeStartTime());
            eq(HrmProgrammeEmployee::getIsAccumulation, 1);
            eq(HrmProgrammeEmployee::getProgrammeState, 1);
        }});
        if (accumulationCount > 0) {
            throw new ResultException("公积金已存在相同的缴纳模版");
        }
        if (programmeEmployeeSaveBo.getProgrammeType() == 1 && programmeEmployeeSaveBo.getAccumulationType() == 1) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            HrmProgrammeEmployee programmeEmployee = new HrmProgrammeEmployee();
            Integer employeeId = programmeEmployeeSaveBo.getEmployeeId();
            EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
            programmeEmployee.setProgrammeId(programmeEmployeeSaveBo.getProgrammeId());
            programmeEmployee.setUserId(employeeDO.getId());
            programmeEmployee.setUserName(employeeDO.getName());
            programmeEmployee.setUserType(employeeDO.getEmploymentType());
            programmeEmployee.setIdNumber(employeeDO.getIdNumber());
            programmeEmployee.setPhone(employeeDO.getMobile());
            programmeEmployee.setDepartmentId(employeeDO.getDeptId());
            programmeEmployee.setProgrammeType(programmeEmployeeSaveBo.getProgrammeType());
            programmeEmployee.setAccountId(programmeEmployeeSaveBo.getAccountId());
            programmeEmployee.setCityId(programmeEmployeeSaveBo.getCityId());
            CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());

            if (!ObjectUtils.isEmpty(dept.getCheckedData())) {
                programmeEmployee.setDepartmentName(dept.getCheckedData().getName());
            }
//            programmeEmployee.setProgrammeState(employeeDO.getState());
//            programmeEmployee.setDateEmployment(dateFormat.format(employeeDO.getEntryTime()));
//            programmeEmployee.seta
            programmeEmployee.setCompanyId(programmeEmployeeSaveBo.getCompanyId());
            programmeEmployee.setIsProgramme(programmeEmployeeSaveBo.getIsProgramme());
            programmeEmployee.setProgrammeStartTime(programmeEmployeeSaveBo.getProgrammeStartTime());
            programmeEmployee.setProgrammeLimit(programmeEmployeeSaveBo.getProgrammeLimit());
            programmeEmployee.setIsAccumulation(programmeEmployeeSaveBo.getIsAccumulation());
            programmeEmployee.setAccumulationStartTime(programmeEmployeeSaveBo.getAccumulationStartTime());
            programmeEmployee.setAccumulationLimit(programmeEmployeeSaveBo.getAccumulationLimit());
            programmeEmployee.setAccumulationType(programmeEmployeeSaveBo.getAccumulationType());
            hrmProgrammeEmployeeMapper.insert(programmeEmployee);
        } else if (programmeEmployeeSaveBo.getProgrammeType() == 1 && programmeEmployeeSaveBo.getAccumulationType() == 2) {
            if (programmeEmployeeSaveBo.getIsAccumulation() == 1) {
                if (!ObjectUtils.isEmpty(programmeEmployeeSaveBo.getAccumulationLimitStr())) {
                    programmeEmployeeSaveBo.getAccumulationLimitStr().forEach(pro -> {
                        Integer insuranceId = pro.getInt("id");

                        String name = pro.getStr("name");
                        BigDecimal amount = pro.getBigDecimal("amount");
                        HrmProgrammeInfo hrmProgrammeInfo = hrmProgrammeInfoMapper.selectById(insuranceId);

                        if (ObjectUtils.isEmpty(hrmProgrammeInfo)) {
                            throw new ResultException(name + "方案公积金不存在");
                        }
                        pro.put("insuranceId", hrmProgrammeInfo.getInsuranceId());
                        if (amount.compareTo(hrmProgrammeInfo.getBaseUpperLimit()) > 0) {
                            throw new ResultException(name + "不能高于方案设置的最大值");
                        } else if (amount.compareTo(hrmProgrammeInfo.getCardinalityLowerLimit()) < 0) {
                            throw new ResultException(name + "不能小于方案设置的最小值");
                        }
                    });
                } else {
                    throw new ResultException("公积金基数不能为空");
                }
            }
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            HrmProgrammeEmployee programmeEmployee = new HrmProgrammeEmployee();
            Integer employeeId = programmeEmployeeSaveBo.getEmployeeId();
            EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
            programmeEmployee.setProgrammeId(programmeEmployeeSaveBo.getProgrammeId());
            programmeEmployee.setUserId(employeeDO.getId());
            programmeEmployee.setUserName(employeeDO.getName());
            programmeEmployee.setUserType(employeeDO.getEmploymentType());
            programmeEmployee.setIdNumber(employeeDO.getIdNumber());
            programmeEmployee.setPhone(employeeDO.getMobile());
            programmeEmployee.setDepartmentId(employeeDO.getDeptId());
            programmeEmployee.setProgrammeType(programmeEmployeeSaveBo.getProgrammeType());
            CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
            if (!ObjectUtils.isEmpty(dept.getCheckedData())) {
                programmeEmployee.setDepartmentName(dept.getCheckedData().getName());
            }
//            programmeEmployee.setProgrammeState(employeeDO.getState());
//            programmeEmployee.setDateEmployment(dateFormat.format(employeeDO.getEntryTime()));
            programmeEmployee.setCompanyId(programmeEmployeeSaveBo.getCompanyId());
            programmeEmployee.setIsProgramme(programmeEmployeeSaveBo.getIsProgramme());
            programmeEmployee.setProgrammeStartTime(programmeEmployeeSaveBo.getProgrammeStartTime());
            programmeEmployee.setProgrammeLimit(programmeEmployeeSaveBo.getProgrammeLimit());
            programmeEmployee.setIsAccumulation(programmeEmployeeSaveBo.getIsAccumulation());
            programmeEmployee.setAccumulationStartTime(programmeEmployeeSaveBo.getAccumulationStartTime());
            programmeEmployee.setAccumulationLimit(programmeEmployeeSaveBo.getAccumulationLimit());
            programmeEmployee.setAccumulationType(programmeEmployeeSaveBo.getAccumulationType());
            programmeEmployee.setAccountId(programmeEmployeeSaveBo.getAccountId());
            programmeEmployee.setCityId(programmeEmployeeSaveBo.getCityId());
            if (!ObjectUtils.isEmpty(programmeEmployeeSaveBo.getAccumulationLimitStr())) {
                programmeEmployee.setAccumulationLimitStr(JSONObject.toJSONString(programmeEmployeeSaveBo.getAccumulationLimitStr()));
            }
            hrmProgrammeEmployeeMapper.insert(programmeEmployee);
        } else if (programmeEmployeeSaveBo.getProgrammeType() == 2 && programmeEmployeeSaveBo.getAccumulationType() == 1) {

            if (programmeEmployeeSaveBo.getIsProgramme() == 1) {
                if (!ObjectUtils.isEmpty(programmeEmployeeSaveBo.getProgrammeLimitStr())) {
                    programmeEmployeeSaveBo.getProgrammeLimitStr().forEach(pro -> {
                        Integer insuranceId = pro.getInt("id");
                        String name = pro.getStr("name");
                        BigDecimal amount = pro.getBigDecimal("amount");
                        HrmProgrammeInfo hrmProgrammeInfo = hrmProgrammeInfoMapper.selectById(insuranceId);
                        if (ObjectUtils.isEmpty(hrmProgrammeInfo)) {
                            throw new ResultException(name + "方案险种不存在");
                        }
                        pro.put("insuranceId", hrmProgrammeInfo.getInsuranceId());
                        if (amount.compareTo(hrmProgrammeInfo.getBaseUpperLimit()) > 0) {
                            throw new ResultException(name + "不能高于方案设置的最大值");
                        } else if (amount.compareTo(hrmProgrammeInfo.getCardinalityLowerLimit()) < 0) {
                            throw new ResultException(name + "不能小于方案设置的最小值");
                        }
                    });
                } else {
                    throw new ResultException("险种基数不能为空");
                }
            }

            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            HrmProgrammeEmployee programmeEmployee = new HrmProgrammeEmployee();
            Integer employeeId = programmeEmployeeSaveBo.getEmployeeId();
            EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
            programmeEmployee.setProgrammeId(programmeEmployeeSaveBo.getProgrammeId());
            programmeEmployee.setUserId(employeeDO.getId());
            programmeEmployee.setUserName(employeeDO.getName());
            programmeEmployee.setUserType(employeeDO.getEmploymentType());
            programmeEmployee.setIdNumber(employeeDO.getIdNumber());
            programmeEmployee.setPhone(employeeDO.getMobile());
            programmeEmployee.setDepartmentId(employeeDO.getDeptId());
            programmeEmployee.setProgrammeType(programmeEmployeeSaveBo.getProgrammeType());
            CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
            programmeEmployee.setAccountId(programmeEmployeeSaveBo.getAccountId());
            programmeEmployee.setCityId(programmeEmployeeSaveBo.getCityId());
            if (!ObjectUtils.isEmpty(dept.getCheckedData())) {
                programmeEmployee.setDepartmentName(dept.getCheckedData().getName());
            }

//            programmeEmployee.setProgrammeState(employeeDO.getState());
//            programmeEmployee.setDateEmployment(dateFormat.format(employeeDO.getEntryTime()));
            programmeEmployee.setCompanyId(programmeEmployeeSaveBo.getCompanyId());
            programmeEmployee.setIsProgramme(programmeEmployeeSaveBo.getIsProgramme());
            programmeEmployee.setProgrammeStartTime(programmeEmployeeSaveBo.getProgrammeStartTime());
            programmeEmployee.setProgrammeLimit(programmeEmployeeSaveBo.getProgrammeLimit());
            programmeEmployee.setIsAccumulation(programmeEmployeeSaveBo.getIsAccumulation());
            programmeEmployee.setAccumulationStartTime(programmeEmployeeSaveBo.getAccumulationStartTime());
            programmeEmployee.setAccumulationLimit(programmeEmployeeSaveBo.getAccumulationLimit());
            programmeEmployee.setAccumulationType(programmeEmployeeSaveBo.getAccumulationType());
            if (!ObjectUtils.isEmpty(programmeEmployeeSaveBo.getProgrammeLimitStr())) {
                programmeEmployee.setProgrammeLimitStr(JSONObject.toJSONString(programmeEmployeeSaveBo.getProgrammeLimitStr()));
            }

            hrmProgrammeEmployeeMapper.insert(programmeEmployee);
        } else {
            if (programmeEmployeeSaveBo.getIsProgramme() == 1) {
                if (!ObjectUtils.isEmpty(programmeEmployeeSaveBo.getProgrammeLimitStr())) {
                    programmeEmployeeSaveBo.getProgrammeLimitStr().forEach(pro -> {
                        Integer insuranceId = pro.getInt("id");
                        String name = pro.getStr("name");
                        BigDecimal amount = pro.getBigDecimal("amount");
                        HrmProgrammeInfo hrmProgrammeInfo = hrmProgrammeInfoMapper.selectById(insuranceId);
                        if (ObjectUtils.isEmpty(hrmProgrammeInfo)) {
                            throw new ResultException(name + "方案险种不存在");
                        }
                        pro.put("insuranceId", hrmProgrammeInfo.getInsuranceId());
                        if (amount.compareTo(hrmProgrammeInfo.getBaseUpperLimit()) > 0) {
                            throw new ResultException(name + "不能高于方案设置的最大值");
                        } else if (amount.compareTo(hrmProgrammeInfo.getCardinalityLowerLimit()) < 0) {
                            throw new ResultException(name + "不能小于方案设置的最小值");
                        }
                    });
                } else {
                    throw new ResultException("险种基数不能为空");
                }
            }

            if (programmeEmployeeSaveBo.getIsAccumulation() == 1) {
                if (!ObjectUtils.isEmpty(programmeEmployeeSaveBo.getAccumulationLimitStr())) {
                    programmeEmployeeSaveBo.getAccumulationLimitStr().forEach(pro -> {
                        Integer insuranceId = pro.getInt("id");
                        String name = pro.getStr("name");
                        BigDecimal amount = pro.getBigDecimal("amount");
                        HrmProgrammeInfo hrmProgrammeInfo = hrmProgrammeInfoMapper.selectById(insuranceId);
                        if (ObjectUtils.isEmpty(hrmProgrammeInfo)) {
                            throw new ResultException(name + "方案公积金不存在");
                        }
                        pro.put("insuranceId", hrmProgrammeInfo.getInsuranceId());
                        if (amount.compareTo(hrmProgrammeInfo.getBaseUpperLimit()) > 0) {
                            throw new ResultException(name + "不能高于方案设置的最大值");
                        } else if (amount.compareTo(hrmProgrammeInfo.getCardinalityLowerLimit()) < 0) {
                            throw new ResultException(name + "不能小于方案设置的最小值");
                        }
                    });
                } else {
                    throw new ResultException("公积金基数不能为空");
                }
            }
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            HrmProgrammeEmployee programmeEmployee = new HrmProgrammeEmployee();
            Integer employeeId = programmeEmployeeSaveBo.getEmployeeId();
            EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
            programmeEmployee.setProgrammeId(programmeEmployeeSaveBo.getProgrammeId());
            programmeEmployee.setUserId(employeeDO.getId());
            programmeEmployee.setUserName(employeeDO.getName());
            programmeEmployee.setUserType(employeeDO.getEmploymentType());
            programmeEmployee.setIdNumber(employeeDO.getIdNumber());
            programmeEmployee.setAccountId(programmeEmployeeSaveBo.getAccountId());
            programmeEmployee.setCityId(programmeEmployeeSaveBo.getCityId());
            programmeEmployee.setPhone(employeeDO.getMobile());
            programmeEmployee.setDepartmentId(employeeDO.getDeptId());
            CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
            if (!ObjectUtils.isEmpty(dept.getCheckedData())) {
                programmeEmployee.setDepartmentName(dept.getCheckedData().getName());
            }
            programmeEmployee.setProgrammeType(programmeEmployeeSaveBo.getProgrammeType());
            programmeEmployee.setAccumulationType(programmeEmployeeSaveBo.getAccumulationType());
            if (!ObjectUtils.isEmpty(programmeEmployeeSaveBo.getProgrammeLimitStr())) {
                programmeEmployee.setProgrammeLimitStr(JSONObject.toJSONString(programmeEmployeeSaveBo.getProgrammeLimitStr()));
            }
//            programmeEmployee.setProgrammeState(employeeDO.getState());
//            programmeEmployee.setDateEmployment(dateFormat.format(employeeDO.getEntryTime()));
            programmeEmployee.setCompanyId(programmeEmployeeSaveBo.getCompanyId());
            programmeEmployee.setIsProgramme(programmeEmployeeSaveBo.getIsProgramme());
            programmeEmployee.setProgrammeStartTime(programmeEmployeeSaveBo.getProgrammeStartTime());
            programmeEmployee.setProgrammeLimit(programmeEmployeeSaveBo.getProgrammeLimit());
            programmeEmployee.setProgrammeType(2);
            programmeEmployee.setIsAccumulation(programmeEmployeeSaveBo.getIsAccumulation());
            programmeEmployee.setAccumulationStartTime(programmeEmployeeSaveBo.getAccumulationStartTime());
            programmeEmployee.setAccumulationLimit(programmeEmployeeSaveBo.getAccumulationLimit());
            if (!ObjectUtils.isEmpty(programmeEmployeeSaveBo.getAccumulationLimitStr())) {
                programmeEmployee.setAccumulationLimitStr(JSONObject.toJSONString(programmeEmployeeSaveBo.getAccumulationLimitStr()));
            }
//

            hrmProgrammeEmployeeMapper.insert(programmeEmployee);
        }

        // 风险点检查
        employeeRiskHandleChainService.execute(Collections.singleton(programmeEmployeeSaveBo.getEmployeeId().longValue()));
        return 1;
    }

    @Override
    public Integer insertProgrammeEmployeeStop(ProgrammeEmployeeSaveBo programmeEmployeeSaveBo) {
        Long programmeCount = hrmProgrammeEmployeeMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getUserId, programmeEmployeeSaveBo.getEmployeeId());
            eq(HrmProgrammeEmployee::getCompanyId, programmeEmployeeSaveBo.getCompanyId());
            eq(HrmProgrammeEmployee::getProgrammeStartTime, programmeEmployeeSaveBo.getProgrammeStartTime());
            eq(HrmProgrammeEmployee::getIsProgramme, 1);
            eq(HrmProgrammeEmployee::getProgrammeState, 1);
        }});
        if (programmeCount > 0) {
            throw new ResultException("社保已存在相同的缴纳模版");
        }
        Long accumulationCount = hrmProgrammeEmployeeMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getUserId, programmeEmployeeSaveBo.getEmployeeId());
            eq(HrmProgrammeEmployee::getCompanyId, programmeEmployeeSaveBo.getCompanyId());
            eq(HrmProgrammeEmployee::getAccumulationStartTime, programmeEmployeeSaveBo.getProgrammeStartTime());
            eq(HrmProgrammeEmployee::getIsAccumulation, 1);
            eq(HrmProgrammeEmployee::getProgrammeState, 1);
        }});
        if (accumulationCount > 0) {
            throw new ResultException("公积金已存在相同的缴纳模版");
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        HrmProgrammeEmployee programmeEmployee = new HrmProgrammeEmployee();
        Integer employeeId = programmeEmployeeSaveBo.getEmployeeId();
        EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
        programmeEmployee.setProgrammeId(programmeEmployeeSaveBo.getProgrammeId());
        programmeEmployee.setUserId(employeeDO.getId());
        programmeEmployee.setUserName(employeeDO.getName());
        programmeEmployee.setUserType(employeeDO.getEmploymentType());
        programmeEmployee.setIdNumber(employeeDO.getIdNumber());
        programmeEmployee.setPhone(employeeDO.getMobile());
        programmeEmployee.setDepartmentId(employeeDO.getDeptId());
        programmeEmployee.setProgrammeState(programmeEmployeeSaveBo.getProgrammeState());
        CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
        if (!ObjectUtils.isEmpty(dept.getCheckedData())) {
            programmeEmployee.setDepartmentName(dept.getCheckedData().getName());
        }

        programmeEmployee.setProgrammeType(programmeEmployeeSaveBo.getProgrammeType());
        programmeEmployee.setAccumulationType(programmeEmployeeSaveBo.getAccumulationType());
//        programmeEmployee.setProgrammeState(employeeDO.getState());
        programmeEmployee.setCityId(programmeEmployeeSaveBo.getCityId());
//        programmeEmployee.setDateEmployment(dateFormat.format(employeeDO.getEntryTime()));
        programmeEmployee.setCompanyId(programmeEmployeeSaveBo.getCompanyId());
        programmeEmployee.setIsProgramme(programmeEmployeeSaveBo.getIsProgramme());
        programmeEmployee.setProgrammeStartTime(programmeEmployeeSaveBo.getProgrammeStartTime());
        programmeEmployee.setProgrammeLimit(programmeEmployeeSaveBo.getProgrammeLimit());
        programmeEmployee.setIsAccumulation(programmeEmployeeSaveBo.getIsAccumulation());
        programmeEmployee.setAccumulationStartTime(programmeEmployeeSaveBo.getAccumulationStartTime());
        programmeEmployee.setAccumulationLimit(programmeEmployeeSaveBo.getAccumulationLimit());
        hrmProgrammeEmployeeMapper.insert(programmeEmployee);

        // 风险点检查
        employeeRiskHandleChainService.execute(Collections.singleton(programmeEmployeeSaveBo.getEmployeeId().longValue()));
        return 1;
    }

    @Override
    public JSONObject selectProgrammeEmployeeInfoListById(Integer employeeId) {
        JSONObject jsonResult = new JSONObject();
        EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
        CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
        if (ObjectUtils.isEmpty(dept.getCheckedData())) {
            jsonResult.put("departmentName", "--");
        } else {
            jsonResult.put("departmentName", dept.getCheckedData().getName());
        }
        jsonResult.put("employeeName", employeeDO.getName());
        jsonResult.put("employeeNo", employeeDO.getEmployeeNo());
        HrmProgrammeEmployee programmeEmployee = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getUserId, employeeId);
            le(HrmProgrammeEmployee::getProgrammeStartTime, DateUtil.currentYearAndMonth());
            eq(HrmProgrammeEmployee::getProgrammeState, 1);
            eq(HrmProgrammeEmployee::getIsProgramme, 1);
            orderByDesc(HrmProgrammeEmployee::getProgrammeStartTime);
            last("limit 1");
        }});

        HrmProgrammeEmployee accumulationEmployee = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getUserId, employeeId);
            le(HrmProgrammeEmployee::getAccumulationStartTime, DateUtil.currentYearAndMonth());
            eq(HrmProgrammeEmployee::getProgrammeState, 1);
            eq(HrmProgrammeEmployee::getIsAccumulation, 1);
            orderByDesc(HrmProgrammeEmployee::getAccumulationStartTime);
            last("limit 1");
        }});

        HrmProgrammeEmployee programmeEmployeeStop = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getUserId, employeeId);
            le(HrmProgrammeEmployee::getProgrammeStartTime, DateUtil.currentYearAndMonth());
            eq(HrmProgrammeEmployee::getProgrammeState, 2);
            eq(HrmProgrammeEmployee::getIsProgramme, 1);
            orderByDesc(HrmProgrammeEmployee::getProgrammeStartTime);
            last("limit 1");
        }});
        HrmProgrammeEmployee accumulationEmployeeStop = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getUserId, employeeId);
            le(HrmProgrammeEmployee::getAccumulationStartTime, DateUtil.currentYearAndMonth());
            eq(HrmProgrammeEmployee::getProgrammeState, 2);
            eq(HrmProgrammeEmployee::getIsAccumulation, 1);
            orderByDesc(HrmProgrammeEmployee::getAccumulationStartTime);
            last("limit 1");
        }});
        Integer state = -1;
        if (ObjectUtils.isEmpty(programmeEmployeeStop) && ObjectUtils.isEmpty(accumulationEmployeeStop)) {
            JSONObject result = new JSONObject();
            //未停止且有当前缴纳社保费
            if (ObjectUtils.isEmpty(programmeEmployeeStop)) {
                System.err.println(programmeEmployee + "空指针");
                if (!ObjectUtils.isEmpty(programmeEmployee) && programmeEmployee.getProgrammeType() == 1) {
                    state = 1;
                    result.put("programmeId", programmeEmployee.getId());
                    result.put("programmeType", 1);
                    if (!ObjectUtils.isEmpty(programmeEmployee)) {
                        result.put("programmeId", programmeEmployee.getId());
                        result.put("programmeEmployeeId", programmeEmployee.getProgrammeId());
                        result.put("programmeLimit", programmeEmployee.getProgrammeLimit());
                        result.put("programmeTime", programmeEmployee.getProgrammeStartTime());
                    } else {
                        result.put("programmeEmployeeId", "-1");
                        result.put("programmeLimit", "--");
                        result.put("programmeTime", "--");
                    }
                } else if (!ObjectUtils.isEmpty(programmeEmployee)) {
                    state = 1;
                    result.put("programmeType", 2);
                    if (!ObjectUtils.isEmpty(programmeEmployee)) {
                        result.put("programmeId", programmeEmployee.getId());
                        result.put("programmeEmployeeId", programmeEmployee.getProgrammeId());
                        result.put("programmeLimit", JSONObject.parseObject(programmeEmployee.getProgrammeLimitStr(), ArrayList.class));
                        result.put("programmeTime", programmeEmployee.getProgrammeStartTime());

                    } else {
                        result.put("programmeEmployeeId", "-1");
                        result.put("programmeLimit", "--");
                        result.put("programmeTime", "--");

                    }
                }
            }
            if (ObjectUtils.isEmpty(accumulationEmployeeStop)) {
                if (!ObjectUtils.isEmpty(accumulationEmployee) && accumulationEmployee.getAccumulationType() == 1) {
                    if (state == 1) {
                        state = 3;
                    } else {
                        state = 2;
                    }
                    result.put("accumulationType", 1);
                    if (!ObjectUtils.isEmpty(accumulationEmployee)) {
                        result.put("programmeId", programmeEmployee.getId());
                        result.put("accumulationEmployeeId", accumulationEmployee.getProgrammeId());
                        result.put("accumulationLimit", accumulationEmployee.getAccumulationLimit());
                        result.put("accumulationTime", accumulationEmployee.getAccumulationStartTime());
                    } else {
                        result.put("accumulationEmployeeId", "-1");
                        result.put("accumulationLimit", "--");
                        result.put("accumulationTime", "--");
                    }
                    jsonResult.put("currentRecord", result);
                } else if (!ObjectUtils.isEmpty(accumulationEmployee)) {

                    result.put("accumulationType", 2);
                    if (!ObjectUtils.isEmpty(accumulationEmployee)) {
                        if (state == 1) {
                            state = 3;
                        } else {
                            state = 2;
                        }
                        result.put("accumulationEmployeeId", accumulationEmployee.getProgrammeId());
                        result.put("accumulationLimit", JSONObject.parseObject(accumulationEmployee.getAccumulationLimitStr(), ArrayList.class));
                        result.put("accumulationTime", accumulationEmployee.getAccumulationStartTime());
                        jsonResult.put("currentRecord", result);
                    } else {
                        result.put("accumulationEmployeeId", "-1");
                        result.put("accumulationLimit", "--");
                        result.put("accumulationTime", "--");
                    }
                }
            }

            result.put("state", state);
            jsonResult.put("currentRecord", result);
        } else {
            JSONObject result = new JSONObject();
            result.put("programmeId", programmeEmployeeStop.getId());
            result.put("state", 4);
            result.put("programmeEmployeeId", "-1");
            result.put("programmeLimit", "停止缴纳");
            result.put("programmeTime", programmeEmployeeStop.getProgrammeStartTime());
            result.put("accumulationEmployeeId", "-1");
            result.put("accumulationLimit", "停止缴纳");
            result.put("accumulationTime", programmeEmployeeStop.getAccumulationStartTime());
            jsonResult.put("currentRecord", result);
        }
        List<JSONObject> jsonObjects = new ArrayList<>();
        List<HrmProgrammeEmployee> allEmployee = new ArrayList<>();
        allEmployee = hrmProgrammeEmployeeMapper.selectList(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getUserId, employeeId);
            orderByDesc(HrmProgrammeEmployee::getId);
        }});

        if (!ObjectUtils.isEmpty(allEmployee)) {
            allEmployee.forEach(ae -> {
                Integer stateNew = -1;
                JSONObject result = new JSONObject();
                result.put("programmeId", ae.getId());
                result.put("programmeReason", ae.getProgrammeReason());
                result.put("state", ae.getUserStatus());
                result.put("programmeType", ae.getProgrammeType());
                result.put("accumulationType", ae.getAccumulationType());
                result.put("programmeState", ae.getProgrammeState());
                if (ae.getProgrammeState() != 2) {
                    if (ae.getIsProgramme() == 1) {
                        stateNew = 1;
                        if (ae.getProgrammeType() == 1) {
                            result.put("programmeLimit", ae.getProgrammeLimit());
                            result.put("programmeTime", ae.getProgrammeStartTime());
                            result.put("programmeReason", ae.getProgrammeReason());
                        } else {
                            result.put("programmeLimit", JSONObject.parseObject(ae.getProgrammeLimitStr(), ArrayList.class));
                            result.put("programmeTime", ae.getProgrammeStartTime());
                            result.put("programmeReason", ae.getProgrammeReason());
                        }

                    }
                    if (ae.getIsAccumulation() == 1) {
                        if (stateNew == 1) {
                            stateNew = 3;
                        } else {
                            stateNew = 2;
                        }
                        if (ae.getAccumulationType() == 1) {
                            result.put("accumulationLimit", ae.getAccumulationLimit());
                            result.put("accumulationTime", ae.getAccumulationStartTime());
                            result.put("programmeReason", ae.getProgrammeReason());
                        } else {
                            result.put("accumulationLimit", JSONObject.parseObject(ae.getAccumulationLimitStr(), ArrayList.class));
                            result.put("accumulationTime", ae.getAccumulationStartTime());
                            result.put("programmeReason", ae.getProgrammeReason());
                        }
                    }
                    result.put("state", stateNew);
                } else {
                    result.put("state", 4);
                    result.put("programmeLimit", "--");
                    result.put("programmeTime", ae.getProgrammeStartTime());
                    result.put("accumulationLimit", "--");
                    result.put("accumulationTime", ae.getAccumulationStartTime());
                    result.put("programmeReason", ae.getProgrammeReason());
                }
                jsonObjects.add(result);
            });
            jsonResult.put("dateList", jsonObjects);
        }
        return jsonResult;
    }

    @Override
    public List<JSONObject> selectCompanySelect(String companyName) {
        List<JSONObject> jsonObjects = new ArrayList<>();

        List<HrmProgrammeCompany> hrmProgrammeCompanies = hrmProgrammeCompanyMapper.selectList(new LambdaQueryWrapper<HrmProgrammeCompany>() {{
            like(!ObjectUtils.isEmpty(companyName), HrmProgrammeCompany::getCompanyName, companyName);
            eq(HrmProgrammeCompany::getState, 1);
        }});
        hrmProgrammeCompanies.forEach(hrmProgrammeCompany -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", hrmProgrammeCompany.getId());
            jsonObject.put("name", hrmProgrammeCompany.getCompanyName());
            jsonObjects.add(jsonObject);
        });
        return jsonObjects;
    }

    @Override
    public JSONObject importProgrammeEmployee(List<ProgrammeEmployeeSaveImportBo> programmeEmployeeSaveImportBos) {
        List<ProgrammeEmployeeSaveImportErrorVo> programmeEmployeeSaveImportBoErroList = new ArrayList<>();
        for (ProgrammeEmployeeSaveImportBo programmeEmployeeSaveImportBo : programmeEmployeeSaveImportBos) {
            ProgrammeEmployeeSaveImportErrorVo errorData = new ProgrammeEmployeeSaveImportErrorVo();
            if (ObjectUtils.isEmpty(programmeEmployeeSaveImportBo)) {
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                errorData.setErrorMessage("导入参保员工不能为空");
                programmeEmployeeSaveImportBoErroList.add(errorData);
                continue;
            }
            if (ObjectUtils.isEmpty(programmeEmployeeSaveImportBo.getUserNo())) {
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                errorData.setErrorMessage("员工编号不能为空");
                programmeEmployeeSaveImportBoErroList.add(errorData);
                continue;
            }
            EmployeeDO employeeDO = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
                eq(EmployeeDO::getEmployeeNo, programmeEmployeeSaveImportBo.getUserNo());
            }});
            if (ObjectUtils.isEmpty(employeeDO)) {
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                errorData.setErrorMessage("员工不存在");
                programmeEmployeeSaveImportBoErroList.add(errorData);
                continue;
            }
            if (ObjectUtils.isEmpty(programmeEmployeeSaveImportBo.getCityName())) {
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                errorData.setErrorMessage("缴纳城市不能为空");
                programmeEmployeeSaveImportBoErroList.add(errorData);
                continue;
            }

            SystemChinaArea systemChinaArea = systemChinaAreaMapper.selectOne(new LambdaQueryWrapper<SystemChinaArea>() {{
                eq(SystemChinaArea::getName, programmeEmployeeSaveImportBo.getCityName());
            }});
            if (ObjectUtils.isEmpty(systemChinaArea)) {
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                errorData.setErrorMessage("缴纳城市不存在");
                programmeEmployeeSaveImportBoErroList.add(errorData);
                continue;
            }


            if (ObjectUtils.isEmpty(programmeEmployeeSaveImportBo.getCompanyName())) {
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                errorData.setErrorMessage("社保公积金缴纳方案不能为空");
                programmeEmployeeSaveImportBoErroList.add(errorData);
                continue;
            }
            HrmProgrammeCompany hrmProgrammeCompany = hrmProgrammeCompanyMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeCompany>() {{
                eq(HrmProgrammeCompany::getCompanyName, programmeEmployeeSaveImportBo.getCompanyName());
            }});

            if (ObjectUtils.isEmpty(hrmProgrammeCompany)) {
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                errorData.setErrorMessage("缴纳公司不存在");
                programmeEmployeeSaveImportBoErroList.add(errorData);
                continue;
            }
            HrmProgramme hrmProgramme = hrmProgrammeMapper.selectOne(new LambdaQueryWrapper<HrmProgramme>() {{
                eq(HrmProgramme::getProgrammeName, programmeEmployeeSaveImportBo.getProgrammeName());
            }});
            if (ObjectUtils.isEmpty(hrmProgramme)) {
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                errorData.setErrorMessage("社保公积金缴纳方案不存在");
                programmeEmployeeSaveImportBoErroList.add(errorData);
                continue;
            }
            if (ObjectUtils.isEmpty(programmeEmployeeSaveImportBo.getIsProgramme())) {
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                errorData.setErrorMessage("社保是否缴纳不能为空");
                programmeEmployeeSaveImportBoErroList.add(errorData);
                continue;
            }
            if (ObjectUtils.isEmpty(programmeEmployeeSaveImportBo.getIsAccumulation())) {
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                errorData.setErrorMessage("公积金是否缴纳不能为空");
                programmeEmployeeSaveImportBoErroList.add(errorData);
                continue;
            }
            if (ObjectUtils.isEmpty(programmeEmployeeSaveImportBo.getAccountName())) {
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                errorData.setErrorMessage("缴纳账户不能为空");
                programmeEmployeeSaveImportBoErroList.add(errorData);
                continue;
            }
            HrmProgrammeAccount hrmProgrammeAccount = hrmProgrammeAccountMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeAccount>() {{
                eq(HrmProgrammeAccount::getAccountName, programmeEmployeeSaveImportBo.getAccountName());
            }});
            if (ObjectUtils.isEmpty(hrmProgrammeAccount)) {
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                errorData.setErrorMessage("缴纳账户不存在");
                programmeEmployeeSaveImportBoErroList.add(errorData);
                continue;
            }
            if (programmeEmployeeSaveImportBo.getIsProgramme().equals("是")) {
                if (ObjectUtils.isEmpty(programmeEmployeeSaveImportBo.getProgrammeStartTime())) {
                    BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                    errorData.setErrorMessage("缴纳社保社保开始缴纳月份必填");
                    programmeEmployeeSaveImportBoErroList.add(errorData);
                    continue;
                }
                if (ObjectUtils.isEmpty(programmeEmployeeSaveImportBo.getProgrammeLimit())) {
                    BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                    errorData.setErrorMessage("缴纳社保社保缴纳基数必填");
                    programmeEmployeeSaveImportBoErroList.add(errorData);
                    continue;
                }
                Long programmeCount = hrmProgrammeEmployeeMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
                    eq(HrmProgrammeEmployee::getUserId, employeeDO.getId());
                    eq(HrmProgrammeEmployee::getCompanyId, hrmProgrammeCompany.getId());
                    eq(HrmProgrammeEmployee::getProgrammeStartTime, programmeEmployeeSaveImportBo.getProgrammeStartTime());
                    eq(HrmProgrammeEmployee::getIsProgramme, 1);
                    eq(HrmProgrammeEmployee::getProgrammeState, 1);
                }});
                if (programmeCount > 0) {
                    BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                    errorData.setErrorMessage("社保已存在相同的缴纳模版");
                    programmeEmployeeSaveImportBoErroList.add(errorData);
                    continue;
                }

                ProgrammeEmployeeSaveBo programmeEmployeeSaveBo = new ProgrammeEmployeeSaveBo();
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, programmeEmployeeSaveBo);
                programmeEmployeeSaveBo.setEmployeeId(employeeDO.getId().intValue());
                programmeEmployeeSaveBo.setCityId(systemChinaArea.getId().intValue());
                programmeEmployeeSaveBo.setCompanyId(hrmProgrammeCompany.getId().intValue());
                programmeEmployeeSaveBo.setAccountId(hrmProgrammeAccount.getId());
                importP(programmeEmployeeSaveBo);

            }
            if (programmeEmployeeSaveImportBo.getIsAccumulation().equals("是")) {

                if (ObjectUtils.isEmpty(programmeEmployeeSaveImportBo.getAccumulationStartTime())) {
                    BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                    errorData.setErrorMessage("缴纳公积金公积金开始缴纳月份必填");
                    programmeEmployeeSaveImportBoErroList.add(errorData);
                    continue;
                }
                if (ObjectUtils.isEmpty(programmeEmployeeSaveImportBo.getAccumulationLimit())) {
                    BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                    errorData.setErrorMessage("缴纳公积金公积金缴纳基数必填");
                    programmeEmployeeSaveImportBoErroList.add(errorData);
                    continue;
                }
                Long accumulationCount = hrmProgrammeEmployeeMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
                    eq(HrmProgrammeEmployee::getUserId, employeeDO.getId());
                    eq(HrmProgrammeEmployee::getCompanyId, hrmProgrammeCompany.getId());
                    eq(HrmProgrammeEmployee::getAccumulationStartTime, programmeEmployeeSaveImportBo.getAccumulationStartTime());
                    eq(HrmProgrammeEmployee::getIsAccumulation, 1);
                    eq(HrmProgrammeEmployee::getProgrammeState, 1);
                }});

                if (accumulationCount > 0) {
                    BeanUtils.copyProperties(programmeEmployeeSaveImportBo, errorData);
                    errorData.setErrorMessage("公积金已存在相同的缴纳模版");
                    programmeEmployeeSaveImportBoErroList.add(errorData);
                    continue;
                }
                ProgrammeEmployeeSaveBo programmeEmployeeSaveBo = new ProgrammeEmployeeSaveBo();
                BeanUtils.copyProperties(programmeEmployeeSaveImportBo, programmeEmployeeSaveBo);
                programmeEmployeeSaveBo.setEmployeeId(employeeDO.getId().intValue());
                programmeEmployeeSaveBo.setCityId(systemChinaArea.getId().intValue());
                programmeEmployeeSaveBo.setCompanyId(hrmProgrammeCompany.getId().intValue());
                programmeEmployeeSaveBo.setAccountId(hrmProgrammeAccount.getId());
                importP(programmeEmployeeSaveBo);
            }

        }
        JSONObject result = new JSONObject();
        result.put("createNames", programmeEmployeeSaveImportBos.size() - programmeEmployeeSaveImportBoErroList.size());
        result.put("failureNames", programmeEmployeeSaveImportBoErroList.size());

        File excel = null;
        try {
            excel = File.createTempFile("Error", ".xls");
            excel.deleteOnExit();
            EasyExcel.write(new FileOutputStream(excel)).sheet("。。。。。").head(ProgrammeEmployeeSaveImportErrorVo.class)
                    .doWrite(programmeEmployeeSaveImportBoErroList);
            result.put("fileName", excel.getName());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.put("fileName", "");
        return result;
    }

    @Override
    public PageInfo<HrmProgrammeCompany> selectPageList(Integer pageNum, Integer pageSize, String companyName) {
        PageHelper.startPage(pageNum, pageSize);
        List<HrmProgrammeCompany> hrmProgrammeCompanies = hrmProgrammeCompanyMapper.selectList(new LambdaQueryWrapper<HrmProgrammeCompany>() {{
            eq(!ObjectUtils.isEmpty(companyName), HrmProgrammeCompany::getCompanyName, companyName);
        }});
        return new PageInfo<>(hrmProgrammeCompanies);
    }

    @Override
    public Integer insertCompany(HrmProgrammeCompany hrmProgrammeCompany) {
        int insert = hrmProgrammeCompanyMapper.insert(hrmProgrammeCompany);
        return insert;
    }

    @Override
    public Integer updateCompany(HrmProgrammeCompany hrmProgrammeCompany) {
        int i = hrmProgrammeCompanyMapper.updateById(hrmProgrammeCompany);
        return i;
    }

    @Override
    public Integer updateState(HrmProgrammeCompany hrmProgrammeCompany) {
        int i = hrmProgrammeCompanyMapper.updateById(hrmProgrammeCompany);
        return i;
    }

    @Override
    public Integer removeCompany(Integer id) {
        Long l = hrmProgrammeEmployeeMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getCompanyId, id);
        }});
        if (l > 0) {
            throw new ResultException("所属公司正在使用不可删除");
        }
        hrmProgrammeCompanyMapper.deleteById(id);
        return 0;
    }

    @Override
    public List<JSONObject> selectProgrammeZDAmount(Integer Id, Integer type) {
        List<HrmProgrammeInsurance> hrmProgrammeInsurances = hrmProgrammeInsuranceMapper.selectList(new LambdaQueryWrapper<HrmProgrammeInsurance>() {{
            eq(HrmProgrammeInsurance::getType, type);
        }});
        List<JSONObject> jsonObjects = new ArrayList<>();
        List<HrmProgrammeInfo> hrmProgrammeInfos = hrmProgrammeInfoMapper.selectList(new LambdaQueryWrapper<HrmProgrammeInfo>().eq(HrmProgrammeInfo::getProgrammeId, Id).in(!ObjectUtils.isEmpty(hrmProgrammeInsurances), HrmProgrammeInfo::getInsuranceId, hrmProgrammeInsurances.stream().map(HrmProgrammeInsurance::getId).collect(Collectors.toList())));
        hrmProgrammeInfos.forEach(hrmProgrammeInfo -> {
            HrmProgrammeInsurance hrmProgrammeInsurance = hrmProgrammeInsuranceMapper.selectById(hrmProgrammeInfo.getInsuranceId());
            JSONObject result = new JSONObject();
            result.put("id", hrmProgrammeInfo.getId());
            result.put("name", hrmProgrammeInsurance.getName());
            result.put("amount", hrmProgrammeInfo.getCardinalityLowerLimit());
            jsonObjects.add(result);
        });
        return jsonObjects;
    }

    @Override
    public PageInfo<HrmProgrammeAccount> selectAccountPageList(Integer pageNum, Integer pageSize, String accountName) {
        PageHelper.startPage(pageNum, pageSize);
        List<HrmProgrammeAccount> hrmProgrammeAccounts = hrmProgrammeAccountMapper.selectList(new LambdaQueryWrapper<HrmProgrammeAccount>() {{
            eq(!ObjectUtils.isEmpty(accountName), HrmProgrammeAccount::getAccountName, accountName);
        }});
        return new PageInfo<>(hrmProgrammeAccounts);
    }

    @Override
    public Integer insertAccount(HrmProgrammeAccount hrmProgrammeAccount) {
        int insert = hrmProgrammeAccountMapper.insert(hrmProgrammeAccount);
        return insert;
    }

    @Override
    public Integer updateAccount(HrmProgrammeAccount hrmProgrammeAccount) {
        int i = hrmProgrammeAccountMapper.updateById(hrmProgrammeAccount);
        return i;
    }

    @Override
    public Integer updateAccountState(HrmProgrammeAccount hrmProgrammeAccount) {
        int i = hrmProgrammeAccountMapper.updateById(hrmProgrammeAccount);
        return i;
    }

    @Override
    public Integer removeAccount(Integer id) {
        Long l = hrmProgrammeEmployeeRecordMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployeeRecord>() {{
            eq(HrmProgrammeEmployeeRecord::getAccountId, id);
        }});
        if (l > 0) {
            throw new ResultException("所属账户正在使用不可删除");
        }
        hrmProgrammeAccountMapper.deleteById(id);
        return 0;
    }

    @Override
    public PageInfo<HrmProgrammeInsurance> selectInsurancePageList(Integer pageNum, Integer pageSize, String name, Integer type) {
        PageHelper.startPage(pageNum, pageSize);
        List<HrmProgrammeInsurance> hrmProgrammeInsurances = hrmProgrammeInsuranceMapper.selectList(new LambdaQueryWrapper<HrmProgrammeInsurance>() {{
            like(HrmProgrammeInsurance::getName, name);
            eq(HrmProgrammeInsurance::getType, type);
        }});

        return new PageInfo<>(hrmProgrammeInsurances);
    }

    @Override
    public Integer insertInsurance(HrmProgrammeInsurance hrmProgrammeInsurance) {
        if(hrmProgrammeInsuranceMapper.exists(new LambdaQueryWrapper<HrmProgrammeInsurance>()
                .eq(HrmProgrammeInsurance::getName, hrmProgrammeInsurance.getName())
                .eq(HrmProgrammeInsurance::getType,hrmProgrammeInsurance.getType())
        )){
            throw new ResultException("社保险种已存在");
        }
        hrmProgrammeInsuranceMapper.insert(hrmProgrammeInsurance);
        return 1;
    }

    @Override
    public Integer updateInsurance(HrmProgrammeInsurance hrmProgrammeInsurance) {
        hrmProgrammeInsuranceMapper.updateById(hrmProgrammeInsurance);
        return 1;
    }

    @Override
    public Integer updateInsuranceState(HrmProgrammeInsurance hrmProgrammeInsurance) {
        hrmProgrammeInsuranceMapper.updateById(hrmProgrammeInsurance);
        return 1;
    }

    @Override
    public Integer removeInsurance(Integer id) {
        hrmProgrammeInsuranceMapper.deleteById(id);
        return 1;
    }

    @Override
    public HrmProgrammeEmployee selectProgrammeEmployeeInfoById(Integer programmeId) {

        HrmProgrammeEmployee programmeEmployee = hrmProgrammeEmployeeMapper.selectById(programmeId);
        if (!ObjectUtils.isEmpty(programmeEmployee.getProgrammeLimitStr())) {
            programmeEmployee.setProgrammeLimitList(JSONObject.parseObject(programmeEmployee.getProgrammeLimitStr(), ArrayList.class));
        }
        if (!ObjectUtils.isEmpty(programmeEmployee.getAccumulationLimitStr())) {
            programmeEmployee.setAccumulationLimitList(JSONObject.parseObject(programmeEmployee.getAccumulationLimitStr(), ArrayList.class));
        }
        return programmeEmployee;
    }


    @Override
    public Integer programmeEmployeeInfoCx(HrmProgrammeEmployeeUpdateBo hrmProgrammeEmployeeUpdateBo) {
        HrmProgrammeEmployee hrmProgrammeEmployee = hrmProgrammeEmployeeMapper.selectById(hrmProgrammeEmployeeUpdateBo.getId());

//        HrmProgrammeEmployee hrmProgrammeEmployee = new HrmProgrammeEmployee();
////        BeanUtils.copyProperties(hrmProgrammeEmployeeUpdateBo, hrmProgrammeEmployee);
//        if (hrmProgrammeEmployeeUpdateBo.getIsProgramme() == 1) {
//            if (hrmProgrammeEmployeeUpdateBo.getProgrammeType() == 2) {
//                hrmProgrammeEmployee.setProgrammeLimitStr(JSONObject.toJSONString(hrmProgrammeEmployeeUpdateBo.getProgrammeLimitStr()));
//            }
//        }
//        if (hrmProgrammeEmployeeUpdateBo.getIsAccumulation() == 1) {
//            if (hrmProgrammeEmployeeUpdateBo.getAccumulationType() == 2) {
//                hrmProgrammeEmployee.setAccumulationLimitStr(JSONObject.toJSONString(hrmProgrammeEmployeeUpdateBo.getAccumulationLimitStr()));
//
//            }
//        }
//        hrmProgrammeEmployee.setProgrammeLimit();

        hrmProgrammeEmployee.setProgrammeState(0);
        hrmProgrammeEmployeeMapper.updateById(hrmProgrammeEmployee);
        // 风险点检查
        employeeRiskHandleChainService.execute(Collections.singleton(hrmProgrammeEmployee.getUserId()));
        return 1;
    }

    @Override
    public Integer programmeEmployeeInfoUpdateById(HrmProgrammeEmployeeUpdateBo hrmProgrammeEmployeeUpdateBo) {

        if(hrmProgrammeEmployeeUpdateBo.getProgrammeState()==2){
            HrmProgrammeEmployee programmeEmployee = hrmProgrammeEmployeeMapper.selectById(hrmProgrammeEmployeeUpdateBo.getId());
            programmeEmployee.setProgrammeState(2);
            programmeEmployee.setProgrammeStartTime(hrmProgrammeEmployeeUpdateBo.getProgrammeStartTime());
            programmeEmployee.setAccumulationStartTime(hrmProgrammeEmployeeUpdateBo.getAccumulationStartTime());
            programmeEmployee.setIsProgramme(hrmProgrammeEmployeeUpdateBo.getIsProgramme());
            programmeEmployee.setIsAccumulation(hrmProgrammeEmployeeUpdateBo.getIsAccumulation());
            hrmProgrammeEmployeeMapper.updateById(programmeEmployee);
        }else {
            Long programmeCount = hrmProgrammeEmployeeMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
                eq(HrmProgrammeEmployee::getUserId, hrmProgrammeEmployeeUpdateBo);
                eq(HrmProgrammeEmployee::getCompanyId, hrmProgrammeEmployeeUpdateBo.getCompanyId());
                eq(HrmProgrammeEmployee::getProgrammeStartTime, hrmProgrammeEmployeeUpdateBo.getProgrammeStartTime());
                eq(HrmProgrammeEmployee::getIsProgramme, 1);
                eq(HrmProgrammeEmployee::getProgrammeState, 1);
                ne(HrmProgrammeEmployee::getId, hrmProgrammeEmployeeUpdateBo.getId());
            }});
            if (programmeCount > 0) {
                throw new ResultException("已存在相同的缴纳方案");
            }
            Long accumulationCount = hrmProgrammeEmployeeMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
                eq(HrmProgrammeEmployee::getUserId, hrmProgrammeEmployeeUpdateBo.getUserId());
                eq(HrmProgrammeEmployee::getCompanyId, hrmProgrammeEmployeeUpdateBo.getCompanyId());
                eq(HrmProgrammeEmployee::getAccumulationStartTime, hrmProgrammeEmployeeUpdateBo.getProgrammeStartTime());
                eq(HrmProgrammeEmployee::getIsAccumulation, 1);
                eq(HrmProgrammeEmployee::getProgrammeState, 1);
                ne(HrmProgrammeEmployee::getId, hrmProgrammeEmployeeUpdateBo.getId());
            }});
            if (accumulationCount > 0) {
                throw new ResultException("已存在相同的缴纳方案");
            }
            if (hrmProgrammeEmployeeUpdateBo.getProgrammeType() == 1 && hrmProgrammeEmployeeUpdateBo.getAccumulationType() == 1) {
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                HrmProgrammeEmployee programmeEmployee = new HrmProgrammeEmployee();
                Integer employeeId = hrmProgrammeEmployeeUpdateBo.getUserId().intValue();
                EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
                programmeEmployee.setProgrammeId(hrmProgrammeEmployeeUpdateBo.getProgrammeId());
                programmeEmployee.setUserId(employeeDO.getId());
                programmeEmployee.setUserName(employeeDO.getName());
                programmeEmployee.setUserType(employeeDO.getEmploymentType());
                programmeEmployee.setIdNumber(employeeDO.getIdNumber());
                programmeEmployee.setPhone(employeeDO.getMobile());
                programmeEmployee.setDepartmentId(employeeDO.getDeptId());
                programmeEmployee.setProgrammeType(hrmProgrammeEmployeeUpdateBo.getProgrammeType());
                programmeEmployee.setAccountId(hrmProgrammeEmployeeUpdateBo.getAccountId());
                programmeEmployee.setCityId(hrmProgrammeEmployeeUpdateBo.getCityId());
                programmeEmployee.setId(hrmProgrammeEmployeeUpdateBo.getId());
                CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());

                if (!ObjectUtils.isEmpty(dept.getCheckedData())) {
                    programmeEmployee.setDepartmentName(dept.getCheckedData().getName());
                }
//            programmeEmployee.setProgrammeState(employeeDO.getState());
//            programmeEmployee.setDateEmployment(dateFormat.format(employeeDO.getEntryTime()));
//            programmeEmployee.seta
                programmeEmployee.setCompanyId(hrmProgrammeEmployeeUpdateBo.getCompanyId());
                programmeEmployee.setIsProgramme(hrmProgrammeEmployeeUpdateBo.getIsProgramme());
                programmeEmployee.setProgrammeStartTime(hrmProgrammeEmployeeUpdateBo.getProgrammeStartTime());
                programmeEmployee.setProgrammeLimit(hrmProgrammeEmployeeUpdateBo.getProgrammeLimit());
                programmeEmployee.setIsAccumulation(hrmProgrammeEmployeeUpdateBo.getIsAccumulation());
                programmeEmployee.setAccumulationStartTime(hrmProgrammeEmployeeUpdateBo.getAccumulationStartTime());
                programmeEmployee.setAccumulationLimit(hrmProgrammeEmployeeUpdateBo.getAccumulationLimit());
                programmeEmployee.setAccumulationType(hrmProgrammeEmployeeUpdateBo.getAccumulationType());
                hrmProgrammeEmployeeMapper.updateById(programmeEmployee);
            } else if (hrmProgrammeEmployeeUpdateBo.getProgrammeType() == 1 && hrmProgrammeEmployeeUpdateBo.getAccumulationType() == 2) {
                if (hrmProgrammeEmployeeUpdateBo.getIsAccumulation() == 1) {
                    if (!ObjectUtils.isEmpty(hrmProgrammeEmployeeUpdateBo.getAccumulationLimitStr())) {
                        hrmProgrammeEmployeeUpdateBo.getAccumulationLimitStr().forEach(pro -> {
                            Integer insuranceId = pro.getInt("id");

                            String name = pro.getStr("name");
                            BigDecimal amount = pro.getBigDecimal("amount");
                            HrmProgrammeInfo hrmProgrammeInfo = hrmProgrammeInfoMapper.selectById(insuranceId);

                            if (ObjectUtils.isEmpty(hrmProgrammeInfo)) {
                                throw new ResultException(name + "方案公积金不存在");
                            }
                            pro.put("insuranceId", hrmProgrammeInfo.getInsuranceId());
                            if (amount.compareTo(hrmProgrammeInfo.getBaseUpperLimit()) > 0) {
                                throw new ResultException(name + "不能高于方案设置的最大值");
                            } else if (amount.compareTo(hrmProgrammeInfo.getCardinalityLowerLimit()) < 0) {
                                throw new ResultException(name + "不能小于方案设置的最小值");
                            }
                        });
                    } else {
                        throw new ResultException("公积金基数不能为空");
                    }
                }
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                HrmProgrammeEmployee programmeEmployee = new HrmProgrammeEmployee();
                Integer employeeId = hrmProgrammeEmployeeUpdateBo.getUserId().intValue();
                EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
                programmeEmployee.setProgrammeId(hrmProgrammeEmployeeUpdateBo.getProgrammeId());
                programmeEmployee.setUserId(employeeDO.getId());
                programmeEmployee.setUserName(employeeDO.getName());
                programmeEmployee.setUserType(employeeDO.getEmploymentType());
                programmeEmployee.setIdNumber(employeeDO.getIdNumber());
                programmeEmployee.setPhone(employeeDO.getMobile());
                programmeEmployee.setDepartmentId(employeeDO.getDeptId());
                programmeEmployee.setProgrammeType(hrmProgrammeEmployeeUpdateBo.getProgrammeType());
                CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
                if (!ObjectUtils.isEmpty(dept.getCheckedData())) {
                    programmeEmployee.setDepartmentName(dept.getCheckedData().getName());
                }
//            programmeEmployee.setProgrammeState(employeeDO.getState());
//            programmeEmployee.setDateEmployment(dateFormat.format(employeeDO.getEntryTime()));
                programmeEmployee.setId(hrmProgrammeEmployeeUpdateBo.getId());
                programmeEmployee.setCompanyId(hrmProgrammeEmployeeUpdateBo.getCompanyId());
                programmeEmployee.setIsProgramme(hrmProgrammeEmployeeUpdateBo.getIsProgramme());
                programmeEmployee.setProgrammeStartTime(hrmProgrammeEmployeeUpdateBo.getProgrammeStartTime());
                programmeEmployee.setProgrammeLimit(hrmProgrammeEmployeeUpdateBo.getProgrammeLimit());
                programmeEmployee.setIsAccumulation(hrmProgrammeEmployeeUpdateBo.getIsAccumulation());
                programmeEmployee.setAccumulationStartTime(hrmProgrammeEmployeeUpdateBo.getAccumulationStartTime());
                programmeEmployee.setAccumulationLimit(hrmProgrammeEmployeeUpdateBo.getAccumulationLimit());
                programmeEmployee.setAccumulationType(hrmProgrammeEmployeeUpdateBo.getAccumulationType());
                programmeEmployee.setAccountId(hrmProgrammeEmployeeUpdateBo.getAccountId());
                programmeEmployee.setCityId(hrmProgrammeEmployeeUpdateBo.getCityId());
                if (!ObjectUtils.isEmpty(hrmProgrammeEmployeeUpdateBo.getAccumulationLimitStr())) {
                    programmeEmployee.setAccumulationLimitStr(JSONObject.toJSONString(hrmProgrammeEmployeeUpdateBo.getAccumulationLimitStr()));
                }
                hrmProgrammeEmployeeMapper.updateById(programmeEmployee);
            } else if (hrmProgrammeEmployeeUpdateBo.getProgrammeType() == 2 && hrmProgrammeEmployeeUpdateBo.getAccumulationType() == 1) {

                if (hrmProgrammeEmployeeUpdateBo.getIsProgramme() == 1) {
                    if (!ObjectUtils.isEmpty(hrmProgrammeEmployeeUpdateBo.getProgrammeLimitStr())) {
                        hrmProgrammeEmployeeUpdateBo.getProgrammeLimitStr().forEach(pro -> {
                            Integer insuranceId = pro.getInt("id");
                            String name = pro.getStr("name");
                            BigDecimal amount = pro.getBigDecimal("amount");
                            HrmProgrammeInfo hrmProgrammeInfo = hrmProgrammeInfoMapper.selectById(insuranceId);
                            if (ObjectUtils.isEmpty(hrmProgrammeInfo)) {
                                throw new ResultException(name + "方案险种不存在");
                            }
                            pro.put("insuranceId", hrmProgrammeInfo.getInsuranceId());
                            if (amount.compareTo(hrmProgrammeInfo.getBaseUpperLimit()) > 0) {
                                throw new ResultException(name + "不能高于方案设置的最大值");
                            } else if (amount.compareTo(hrmProgrammeInfo.getCardinalityLowerLimit()) < 0) {
                                throw new ResultException(name + "不能小于方案设置的最小值");
                            }
                        });
                    } else {
                        throw new ResultException("险种基数不能为空");
                    }
                }

                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                HrmProgrammeEmployee programmeEmployee = new HrmProgrammeEmployee();
                Integer employeeId = hrmProgrammeEmployeeUpdateBo.getUserId().intValue();
                EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
                programmeEmployee.setProgrammeId(hrmProgrammeEmployeeUpdateBo.getProgrammeId());
                programmeEmployee.setUserId(employeeDO.getId());
                programmeEmployee.setUserName(employeeDO.getName());
                programmeEmployee.setUserType(employeeDO.getEmploymentType());
                programmeEmployee.setIdNumber(employeeDO.getIdNumber());
                programmeEmployee.setPhone(employeeDO.getMobile());
                programmeEmployee.setDepartmentId(employeeDO.getDeptId());
                programmeEmployee.setProgrammeType(hrmProgrammeEmployeeUpdateBo.getProgrammeType());
                CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
                programmeEmployee.setAccountId(hrmProgrammeEmployeeUpdateBo.getAccountId());
                programmeEmployee.setCityId(hrmProgrammeEmployeeUpdateBo.getCityId());
                if (!ObjectUtils.isEmpty(dept.getCheckedData())) {
                    programmeEmployee.setDepartmentName(dept.getCheckedData().getName());
                }

//            programmeEmployee.setProgrammeState(employeeDO.getState());
//            programmeEmployee.setDateEmployment(dateFormat.format(employeeDO.getEntryTime()));
                programmeEmployee.setCompanyId(hrmProgrammeEmployeeUpdateBo.getCompanyId());
                programmeEmployee.setIsProgramme(hrmProgrammeEmployeeUpdateBo.getIsProgramme());
                programmeEmployee.setProgrammeStartTime(hrmProgrammeEmployeeUpdateBo.getProgrammeStartTime());
                programmeEmployee.setProgrammeLimit(hrmProgrammeEmployeeUpdateBo.getProgrammeLimit());
                programmeEmployee.setIsAccumulation(hrmProgrammeEmployeeUpdateBo.getIsAccumulation());
                programmeEmployee.setAccumulationStartTime(hrmProgrammeEmployeeUpdateBo.getAccumulationStartTime());
                programmeEmployee.setAccumulationLimit(hrmProgrammeEmployeeUpdateBo.getAccumulationLimit());
                programmeEmployee.setAccumulationType(hrmProgrammeEmployeeUpdateBo.getAccumulationType());
                if (!ObjectUtils.isEmpty(hrmProgrammeEmployeeUpdateBo.getProgrammeLimitStr())) {
                    programmeEmployee.setProgrammeLimitStr(JSONObject.toJSONString(hrmProgrammeEmployeeUpdateBo.getProgrammeLimitStr()));
                }
                programmeEmployee.setId(hrmProgrammeEmployeeUpdateBo.getId());
                hrmProgrammeEmployeeMapper.updateById(programmeEmployee);
            } else {
                if (hrmProgrammeEmployeeUpdateBo.getIsProgramme() == 1) {
                    if (!ObjectUtils.isEmpty(hrmProgrammeEmployeeUpdateBo.getProgrammeLimitStr())) {
                        hrmProgrammeEmployeeUpdateBo.getProgrammeLimitStr().forEach(pro -> {
                            Integer insuranceId = pro.getInt("id");
                            String name = pro.getStr("name");
                            BigDecimal amount = pro.getBigDecimal("amount");
                            HrmProgrammeInfo hrmProgrammeInfo = hrmProgrammeInfoMapper.selectById(insuranceId);
                            if (ObjectUtils.isEmpty(hrmProgrammeInfo)) {
                                throw new ResultException(name + "方案险种不存在");
                            }
                            pro.put("insuranceId", hrmProgrammeInfo.getInsuranceId());
                            if (amount.compareTo(hrmProgrammeInfo.getBaseUpperLimit()) > 0) {
                                throw new ResultException(name + "不能高于方案设置的最大值");
                            } else if (amount.compareTo(hrmProgrammeInfo.getCardinalityLowerLimit()) < 0) {
                                throw new ResultException(name + "不能小于方案设置的最小值");
                            }
                        });
                    } else {
                        throw new ResultException("险种基数不能为空");
                    }
                }

                if (hrmProgrammeEmployeeUpdateBo.getIsAccumulation() == 1) {
                    if (!ObjectUtils.isEmpty(hrmProgrammeEmployeeUpdateBo.getAccumulationLimitStr())) {
                        hrmProgrammeEmployeeUpdateBo.getAccumulationLimitStr().forEach(pro -> {
                            Integer insuranceId = pro.getInt("id");
                            String name = pro.getStr("name");
                            BigDecimal amount = pro.getBigDecimal("amount");
                            HrmProgrammeInfo hrmProgrammeInfo = hrmProgrammeInfoMapper.selectById(insuranceId);
                            if (ObjectUtils.isEmpty(hrmProgrammeInfo)) {
                                throw new ResultException(name + "方案公积金不存在");
                            }
                            pro.put("insuranceId", hrmProgrammeInfo.getInsuranceId());
                            if (amount.compareTo(hrmProgrammeInfo.getBaseUpperLimit()) > 0) {
                                throw new ResultException(name + "不能高于方案设置的最大值");
                            } else if (amount.compareTo(hrmProgrammeInfo.getCardinalityLowerLimit()) < 0) {
                                throw new ResultException(name + "不能小于方案设置的最小值");
                            }
                        });
                    } else {
                        throw new ResultException("公积金基数不能为空");
                    }
                }
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                HrmProgrammeEmployee programmeEmployee = new HrmProgrammeEmployee();
                Integer employeeId = hrmProgrammeEmployeeUpdateBo.getUserId().intValue();
                EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
                programmeEmployee.setProgrammeId(hrmProgrammeEmployeeUpdateBo.getProgrammeId());
                programmeEmployee.setUserId(employeeDO.getId());
                programmeEmployee.setUserName(employeeDO.getName());
                programmeEmployee.setUserType(employeeDO.getEmploymentType());
                programmeEmployee.setIdNumber(employeeDO.getIdNumber());
                programmeEmployee.setAccountId(hrmProgrammeEmployeeUpdateBo.getAccountId());
                programmeEmployee.setCityId(hrmProgrammeEmployeeUpdateBo.getCityId());
                programmeEmployee.setPhone(employeeDO.getMobile());
                programmeEmployee.setDepartmentId(employeeDO.getDeptId());
                CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
                if (!ObjectUtils.isEmpty(dept.getCheckedData())) {
                    programmeEmployee.setDepartmentName(dept.getCheckedData().getName());
                }
                programmeEmployee.setProgrammeType(hrmProgrammeEmployeeUpdateBo.getProgrammeType());
                programmeEmployee.setAccumulationType(hrmProgrammeEmployeeUpdateBo.getAccumulationType());
                if (!ObjectUtils.isEmpty(hrmProgrammeEmployeeUpdateBo.getProgrammeLimitStr())) {
                    programmeEmployee.setProgrammeLimitStr(JSONObject.toJSONString(hrmProgrammeEmployeeUpdateBo.getProgrammeLimitStr()));
                }
//            programmeEmployee.setProgrammeState(employeeDO.getState());
//            programmeEmployee.setDateEmployment(dateFormat.format(employeeDO.getEntryTime()));
                programmeEmployee.setCompanyId(hrmProgrammeEmployeeUpdateBo.getCompanyId());
                programmeEmployee.setIsProgramme(hrmProgrammeEmployeeUpdateBo.getIsProgramme());
                programmeEmployee.setProgrammeStartTime(hrmProgrammeEmployeeUpdateBo.getProgrammeStartTime());
                programmeEmployee.setProgrammeLimit(hrmProgrammeEmployeeUpdateBo.getProgrammeLimit());
                programmeEmployee.setProgrammeType(2);
                programmeEmployee.setIsAccumulation(hrmProgrammeEmployeeUpdateBo.getIsAccumulation());
                programmeEmployee.setAccumulationStartTime(hrmProgrammeEmployeeUpdateBo.getAccumulationStartTime());
                programmeEmployee.setAccumulationLimit(hrmProgrammeEmployeeUpdateBo.getAccumulationLimit());
                if (!ObjectUtils.isEmpty(hrmProgrammeEmployeeUpdateBo.getAccumulationLimitStr())) {
                    programmeEmployee.setAccumulationLimitStr(JSONObject.toJSONString(hrmProgrammeEmployeeUpdateBo.getAccumulationLimitStr()));
                }
                programmeEmployee.setId(hrmProgrammeEmployeeUpdateBo.getId());
//

                hrmProgrammeEmployeeMapper.updateById(programmeEmployee);
            }
        }

        // 风险点检查
        HrmProgrammeEmployee programmeEmployee = hrmProgrammeEmployeeMapper.selectById(hrmProgrammeEmployeeUpdateBo.getId());
        employeeRiskHandleChainService.execute(Collections.singleton(programmeEmployee.getUserId()));
        return 1;
    }

    @Override
    public JSONObject selectByEmployee(Integer employeeId) {
        JSONObject result = new JSONObject();
        EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
        String employeeNo = employeeDO.getEmployeeNo();
        result.put("employeeNo", employeeNo);
        result.put("employeeName", employeeDO.getName());
        CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
        if (!ObjectUtils.isEmpty(dept.getCheckedData())) {
            result.put("departmentName", dept.getCheckedData().getName());
        }
        return result;
    }


    @Override
    public List<JSONObject> selectGRHJAmount(String year) {
        String yearStartTime = year + "-01";
        String yearEndTime = year + "-12";
        //合计金额
        List<JSONObject> jsonObjects = hrmProgrammeEmployeeRecordMapper.selectGRHJAmount(yearStartTime, yearEndTime);
        return jsonObjects;
    }

    @Override
    public PageInfo<JSONObject> selectAccountCompanyBill(String month, String companyName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        //合计金额
        List<JSONObject> jsonObjects = hrmProgrammeEmployeeRecordMapper.selectZDByCompany(month, companyName);
        return new PageInfo<>(jsonObjects);
    }

    @Override
    public PageInfo<JSONObject> selectZDByEmployeeBill(String month, String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        //合计金额
        List<JSONObject> jsonObjects = hrmProgrammeEmployeeRecordMapper.selectZDByEmployee(month, name);
        return new PageInfo<>(jsonObjects);
    }

    @Override
    public PageInfo<JSONObject> selectZDByEmployeeAndCompany(String month, Integer pageNum, Integer pageSize, Integer companyId) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> jsonObjectList = hrmProgrammeEmployeeRecordMapper.selectZDByEmployeeAndCompany(month, companyId);
        return new PageInfo<>(jsonObjectList);
    }

    public Map selectByEmployeeIdZdMX(Integer employeeId, String month) {
        JSONObject result = new JSONObject();
        EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
        result.put("employeeName", employeeDO.getName());

        CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
        if (!ObjectUtils.isEmpty(dept.getCheckedData())) {
            result.put("departmentName", dept.getCheckedData().getName());
        }
        HrmProgrammeEmployeeRecord hrmProgrammeEmployeeRecord = hrmProgrammeEmployeeRecordMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployeeRecord>() {{
            eq(HrmProgrammeEmployeeRecord::getEmployeeId, employeeId);
            eq(HrmProgrammeEmployeeRecord::getProgrammeStartTime, month);
            last("limit 1");
        }});

        if (ObjectUtils.isEmpty(hrmProgrammeEmployeeRecord)) {
            return new JSONObject();
        }
        result.put("HjAmount", hrmProgrammeEmployeeRecord.getHjAmount());
        ObjectMapper mapper = new ObjectMapper();
        try {
            Map map = mapper.readValue(hrmProgrammeEmployeeRecord.getDataStr(), Map.class);
            System.err.println(map.toString());
            return map;
        } catch (JsonProcessingException e) {
            return new HashMap<>();
        }

    }


    public JSONObject generateEmployeeIdZdMX(Integer employeeId, String month, Integer pEmployeeId, Integer aEmployeeId) {

        JSONObject result = new JSONObject();
        result.put("gJJGRHj", BigDecimal.ZERO);
        result.put("sBGRHj", BigDecimal.ZERO);
//        EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
//        result.put("employeeName", employeeDO.getName());
//
//        CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
//        if (!ObjectUtils.isEmpty(dept.getCheckedData())) {
//            result.put("departmentName", dept.getCheckedData().getName());
//        }
//        HrmProgrammeEmployeeRecord hrmProgrammeEmployeeRecord = hrmProgrammeEmployeeRecordMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployeeRecord>() {{
//            eq(HrmProgrammeEmployeeRecord::getEmployeeId, employeeId);
//            eq(HrmProgrammeEmployeeRecord::getProgrammeStartTime, month);
//            last("limit 1");
//        }});

//        if (ObjectUtils.isEmpty(hrmProgrammeEmployeeRecord)) {
//            return new JSONObject();
//        }
//        result.put("id", hrmProgrammeEmployeeRecord.getId());
//        result.put("remark", hrmProgrammeEmployeeRecord.getRemark());
//        result.put("isUpdate", hrmProgrammeEmployeeRecord.getIsUpdate());
        List<HrmProgrammeInsurance> hrmProgrammeInsurances = hrmProgrammeInsuranceMapper.selectList(new LambdaQueryWrapper<HrmProgrammeInsurance>() {{
            eq(HrmProgrammeInsurance::getType, 1);
        }});
//        if(!ObjectUtils.isEmpty(hrmProgrammeEmployeeRecord)){
        if (!ObjectUtils.isEmpty(pEmployeeId)) {
            HrmProgrammeEmployee programmeEmployee = hrmProgrammeEmployeeMapper.selectById(pEmployeeId);
            HrmProgramme hrmProgramme = hrmProgrammeMapper.selectById(programmeEmployee.getProgrammeId());
            if (programmeEmployee.getIsProgramme() == 1) {
                if (programmeEmployee.getProgrammeType() == 1) {
                    List<HrmProgrammeInfo> hrmProgrammeInfoSbs = hrmProgrammeInfoMapper.selectList(new LambdaQueryWrapper<HrmProgrammeInfo>() {{
                        eq(HrmProgrammeInfo::getProgrammeId, hrmProgramme.getId());
                        in(HrmProgrammeInfo::getInsuranceId, hrmProgrammeInsurances.stream().map(HrmProgrammeInsurance::getId).collect(Collectors.toList()));
                    }});
                    List<JSONObject> resultList = new ArrayList<>();
                    BigDecimal sBGRHj = new BigDecimal(0);
                    BigDecimal sBGSHJ = new BigDecimal(0);
                    for (HrmProgrammeInfo hrmProgrammeInfo : hrmProgrammeInfoSbs) {
                        BigDecimal employeeJNAmount = new BigDecimal(0);
                        BigDecimal companyAmount = new BigDecimal(0);
                        HrmProgrammeInsurance employeeHrmProgrammeInsurance = hrmProgrammeInsuranceMapper.selectById(hrmProgrammeInfo.getInsuranceId());
                        JSONObject jsonObject = new JSONObject();
                        HrmProgrammeRoundUp employeeHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmProgrammeInfo.getIndividualRule());
                        HrmProgrammeRoundUp companyHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmProgrammeInfo.getCompanyRule());
                        JSONObject putResult = new JSONObject();
                        putResult.put("name", employeeHrmProgrammeInsurance.getName());
                        if (employeeHrmProgrammeRoundUp.getType() == 1) {
                            employeeJNAmount = programmeEmployee.getProgrammeLimit().multiply(new BigDecimal(hrmProgrammeInfo.getIndividualProportion()).divide(new BigDecimal(100))).setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                        } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                            employeeJNAmount = programmeEmployee.getProgrammeLimit().multiply(new BigDecimal(hrmProgrammeInfo.getIndividualProportion()).divide(new BigDecimal(100))).setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);
                        } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                            employeeJNAmount = programmeEmployee.getProgrammeLimit().multiply(new BigDecimal(hrmProgrammeInfo.getIndividualProportion()).divide(new BigDecimal(100))).setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);
                        }
                        sBGRHj = sBGRHj.add(employeeJNAmount);
                        putResult.put("amount", employeeJNAmount + "");
                        putResult.put("uuId", UUID.randomUUID());
                        putResult.put("name", employeeHrmProgrammeInsurance.getName());
                        putResult.put("limit", programmeEmployee.getProgrammeLimit() + "");
                        putResult.put("individualProportion", hrmProgrammeInfo.getIndividualProportion() + "%");
                        putResult.put("zName", "个人");
                        jsonObject.put("zName", putResult);
                        resultList.add(putResult);
                        if (companyHrmProgrammeRoundUp.getType() == 1) {
                            companyAmount = programmeEmployee.getProgrammeLimit().multiply(new BigDecimal(hrmProgrammeInfo.getCompanyProportion()).divide(new BigDecimal(100))).setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                        } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                            companyAmount = programmeEmployee.getProgrammeLimit().multiply(new BigDecimal(hrmProgrammeInfo.getCompanyProportion()).divide(new BigDecimal(100))).setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);
                        } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                            companyAmount = programmeEmployee.getProgrammeLimit().multiply(new BigDecimal(hrmProgrammeInfo.getCompanyProportion()).divide(new BigDecimal(100))).setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);
                        }
                        sBGSHJ = sBGSHJ.add(companyAmount);
                        putResult = new JSONObject();
                        putResult.put("uuId", UUID.randomUUID());
                        putResult.put("name", employeeHrmProgrammeInsurance.getName());
                        putResult.put("amount", companyAmount + "");
                        putResult.put("limit", programmeEmployee.getProgrammeLimit() + "");
                        putResult.put("individualProportion", hrmProgrammeInfo.getCompanyProportion() + "%");
                        putResult.put("zName", "公司");
                        resultList.add(putResult);
                    }
                    HrmProgrammeRoundUp employeeHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmProgramme.getIndividualRule());
                    HrmProgrammeRoundUp companyHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmProgramme.getCompanyRule());
                    if (companyHrmProgrammeRoundUp.getType() == 1) {
                        sBGSHJ = sBGSHJ.setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                    } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                        sBGSHJ = sBGSHJ.setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);
                    } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                        sBGSHJ = sBGSHJ.setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);
                    }
                    if (employeeHrmProgrammeRoundUp.getType() == 1) {
                        sBGRHj = sBGRHj.setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                    } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                        sBGRHj = sBGRHj.setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);

                    } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                        sBGRHj = sBGRHj.setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);
                    }
//                result.put("programme", resultList + "");
//                result.put("programmeGRHj", sBGRHj + "");
//                result.put("programmeGSHJ", sBGSHJ + "");
//                result.put("programmeHJ", sBGSHJ.add(sBGRHj) + "");
//                hrmProgrammeEmployeeRecord.setProgrammeAmount(sBGSHJ);
//                hrmProgrammeEmployeeRecord.setEmployeeProgrammeAmount(sBGRHj);
                } else {
                    List<HrmProgrammeInfo> hrmProgrammeInfoSbs = hrmProgrammeInfoMapper.selectList(new LambdaQueryWrapper<HrmProgrammeInfo>() {{
                        eq(HrmProgrammeInfo::getProgrammeId, hrmProgramme.getId());
                        in(HrmProgrammeInfo::getInsuranceId, hrmProgrammeInsurances.stream().map(HrmProgrammeInsurance::getId).collect(Collectors.toList()));
                    }});
                    List<JSONObject> resultList = new ArrayList<>();
                    BigDecimal sBGRHj = new BigDecimal(0);
                    BigDecimal sBGSHJ = new BigDecimal(0);
                    for (HrmProgrammeInfo hrmProgrammeInfo : hrmProgrammeInfoSbs) {
                        Integer limit = 0;
                        BigDecimal employeeJNAmount = new BigDecimal(0);
                        BigDecimal companyAmount = new BigDecimal(0);
                        HrmProgrammeInsurance employeeHrmProgrammeInsurance = hrmProgrammeInsuranceMapper.selectById(hrmProgrammeInfo.getInsuranceId());
                        JSONObject jsonObject = new JSONObject();
                        HrmProgrammeRoundUp employeeHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmProgrammeInfo.getIndividualRule());
                        HrmProgrammeRoundUp companyHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmProgrammeInfo.getCompanyRule());
                        JSONObject putResult = new JSONObject();
                        putResult.put("name", employeeHrmProgrammeInsurance.getName());
                        ArrayList arrayList = JSONObject.parseObject(programmeEmployee.getProgrammeLimitStr(), ArrayList.class);
                        for (Object j : arrayList) {
                            JSONObject jsonObject1 = JSONObject.parseObject(JSONObject.toJSONString(j));
                            if (jsonObject1.getInteger("insuranceId") == employeeHrmProgrammeInsurance.getId()) {
                                limit = jsonObject1.getInteger("amount");
                                break;
                            }
                        }
                        if (employeeHrmProgrammeRoundUp.getType() == 1) {
                            employeeJNAmount = new BigDecimal(limit).multiply(new BigDecimal(hrmProgrammeInfo.getIndividualProportion()).divide(new BigDecimal(100))).setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                        } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                            employeeJNAmount = new BigDecimal(limit).multiply(new BigDecimal(hrmProgrammeInfo.getIndividualProportion()).divide(new BigDecimal(100))).setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);

                        } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                            employeeJNAmount = new BigDecimal(limit).multiply(new BigDecimal(hrmProgrammeInfo.getIndividualProportion()).divide(new BigDecimal(100))).setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);

                        }
                        sBGRHj = sBGRHj.add(employeeJNAmount);
                        putResult.put("uuId", UUID.randomUUID());
                        putResult.put("amount", employeeJNAmount + "");
                        putResult.put("name", employeeHrmProgrammeInsurance.getName() + "");
                        putResult.put("limit", limit + "");
                        putResult.put("individualProportion", hrmProgrammeInfo.getIndividualProportion() + "%");
                        putResult.put("zName", "个人");
                        jsonObject.put("zName", putResult);
                        resultList.add(putResult);
                        if (companyHrmProgrammeRoundUp.getType() == 1) {
                            companyAmount = new BigDecimal(limit).multiply(new BigDecimal(hrmProgrammeInfo.getCompanyProportion()).divide(new BigDecimal(100))).setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                        } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                            companyAmount = new BigDecimal(limit).multiply(new BigDecimal(hrmProgrammeInfo.getCompanyProportion()).divide(new BigDecimal(100))).setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);
                        } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                            companyAmount = new BigDecimal(limit).multiply(new BigDecimal(hrmProgrammeInfo.getCompanyProportion()).divide(new BigDecimal(100))).setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);
                        }
                        sBGSHJ = sBGSHJ.add(companyAmount);
                        putResult = new JSONObject();
                        putResult.put("uuId", UUID.randomUUID());
                        putResult.put("name", employeeHrmProgrammeInsurance.getName());
                        putResult.put("amount", companyAmount + "");
                        putResult.put("limit", new BigDecimal(limit) + "");
                        putResult.put("individualProportion", hrmProgrammeInfo.getCompanyProportion() + "%");
                        putResult.put("zName", "公司");
                        resultList.add(putResult);
                    }
                    HrmProgrammeRoundUp employeeHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmProgramme.getIndividualRule());
                    HrmProgrammeRoundUp companyHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmProgramme.getCompanyRule());

                    if (companyHrmProgrammeRoundUp.getType() == 1) {
                        sBGSHJ = sBGSHJ.setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                    } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                        sBGSHJ = sBGSHJ.setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);
                    } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                        sBGSHJ = sBGSHJ.setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);
                    }

                    if (employeeHrmProgrammeRoundUp.getType() == 1) {
                        sBGRHj = sBGRHj.setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                    } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                        sBGRHj = sBGRHj.setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);

                    } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                        sBGRHj = sBGRHj.setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);

                    }
//                result.put("programme", resultList);
//                result.put("programmeGRHj", sBGRHj + "");
//                result.put("programmeGSHJ", sBGSHJ + "");
//                result.put("programmeHJ", sBGSHJ.add(sBGRHj) + "");
//                hrmProgrammeEmployeeRecord.setProgrammeAmount(sBGSHJ);
//                hrmProgrammeEmployeeRecord.setEmployeeProgrammeAmount(sBGRHj);

                    result.put("sBGRHj", sBGRHj);
                }
//            hrmProgrammeEmployeeRecordMapper.updateById(hrmProgrammeEmployeeRecord);
            } else {
//            hrmProgrammeEmployeeRecord.setProgrammeAmount(new BigDecimal(0));
//            hrmProgrammeEmployeeRecord.setEmployeeProgrammeAmount(new BigDecimal(0));
//            result.put("programme", new ArrayList<>());
//            result.put("programmeGRHj", 0);
//            result.put("programmeGSHJ", 0);
//            result.put("programmeHJ", 0);
//            hrmProgrammeEmployeeRecordMapper.updateById(hrmProgrammeEmployeeRecord);
                result.put("sBGRHj", BigDecimal.ZERO);
            }
        }
////        }
//        HrmProgrammeEmployeeRecord hrmProgrammeEmployeeRecordA = hrmProgrammeEmployeeRecordMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployeeRecord>() {{
//            eq(HrmProgrammeEmployeeRecord::getEmployeeId, employeeId);
//            eq(HrmProgrammeEmployeeRecord::getAccumulationStartTime, month);
//            last("limit 1");
//        }});
        List<HrmProgrammeInsurance> hrmAccumulationInsurances = hrmProgrammeInsuranceMapper.selectList(new LambdaQueryWrapper<HrmProgrammeInsurance>() {{
            eq(HrmProgrammeInsurance::getType, 2);
        }});
        if (!ObjectUtils.isEmpty(aEmployeeId)) {
            HrmProgrammeEmployee accumulationEmployee = hrmProgrammeEmployeeMapper.selectById(aEmployeeId);
            HrmProgramme hrmAccumulation = hrmProgrammeMapper.selectById(accumulationEmployee.getProgrammeId());
            if (accumulationEmployee.getIsAccumulation() == 1) {
                if (accumulationEmployee.getAccumulationType() == 1) {
                    List<HrmProgrammeInfo> hrmProgrammeInfoSbs = hrmProgrammeInfoMapper.selectList(new LambdaQueryWrapper<HrmProgrammeInfo>() {{
                        eq(HrmProgrammeInfo::getProgrammeId, hrmAccumulation.getId());
                        in(HrmProgrammeInfo::getInsuranceId, hrmAccumulationInsurances.stream().map(HrmProgrammeInsurance::getId).collect(Collectors.toList()));
                    }});
                    List<JSONObject> resultList = new ArrayList<>();
                    BigDecimal gJJGRHj = new BigDecimal(0);
                    BigDecimal gJJGSHJ = new BigDecimal(0);
                    for (HrmProgrammeInfo hrmProgrammeInfo : hrmProgrammeInfoSbs) {
                        BigDecimal employeeJNAmount = new BigDecimal(0);
                        BigDecimal companyAmount = new BigDecimal(0);
                        HrmProgrammeInsurance employeeHrmProgrammeInsurance = hrmProgrammeInsuranceMapper.selectById(hrmProgrammeInfo.getInsuranceId());
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("name", employeeHrmProgrammeInsurance.getName());
                        HrmProgrammeRoundUp employeeHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmProgrammeInfo.getIndividualRule());
                        HrmProgrammeRoundUp companyHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmProgrammeInfo.getCompanyRule());
                        JSONObject putResult = new JSONObject();
                        if (employeeHrmProgrammeRoundUp.getType() == 1) {
                            employeeJNAmount = accumulationEmployee.getAccumulationLimit().multiply(new BigDecimal(hrmProgrammeInfo.getIndividualProportion()).divide(new BigDecimal(100))).setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                        } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                            employeeJNAmount = accumulationEmployee.getAccumulationLimit().multiply(new BigDecimal(hrmProgrammeInfo.getIndividualProportion()).divide(new BigDecimal(100))).setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);

                        } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                            employeeJNAmount = accumulationEmployee.getAccumulationLimit().multiply(new BigDecimal(hrmProgrammeInfo.getIndividualProportion()).divide(new BigDecimal(100))).setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);
                        }
                        gJJGRHj = gJJGRHj.add(employeeJNAmount);
                        putResult.put("uuId", UUID.randomUUID());
                        putResult.put("amount", employeeJNAmount + "");
                        putResult.put("name", employeeHrmProgrammeInsurance.getName());
                        putResult.put("limit", accumulationEmployee.getAccumulationLimit() + "");
                        putResult.put("individualProportion", hrmProgrammeInfo.getIndividualProportion() + "%");
                        putResult.put("zName", "个人");
                        jsonObject.put("individualData", putResult);
                        resultList.add(putResult);
                        if (companyHrmProgrammeRoundUp.getType() == 1) {
                            companyAmount = accumulationEmployee.getAccumulationLimit().multiply(new BigDecimal(hrmProgrammeInfo.getCompanyProportion()).divide(new BigDecimal(100))).setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                        } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                            companyAmount = accumulationEmployee.getAccumulationLimit().multiply(new BigDecimal(hrmProgrammeInfo.getCompanyProportion()).divide(new BigDecimal(100))).setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);
                        } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                            companyAmount = accumulationEmployee.getAccumulationLimit().multiply(new BigDecimal(hrmProgrammeInfo.getCompanyProportion()).divide(new BigDecimal(100))).setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);
                        }
                        gJJGSHJ = gJJGSHJ.add(companyAmount);
                        putResult = new JSONObject();
                        putResult.put("uuId", UUID.randomUUID());
                        putResult.put("name", employeeHrmProgrammeInsurance.getName());
                        putResult.put("amount", companyAmount + "");
                        putResult.put("limit", accumulationEmployee.getAccumulationLimit() + "");
                        putResult.put("individualProportion", hrmProgrammeInfo.getCompanyProportion() + "%");
                        putResult.put("zName", "公司");
                        jsonObject.put("companyData", putResult);
                        jsonObject.put("JNZE", companyAmount.add(employeeJNAmount) + "");
                        resultList.add(putResult);
                    }
                    HrmProgrammeRoundUp employeeHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmAccumulation.getGjjIndividualRule());
                    HrmProgrammeRoundUp companyHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmAccumulation.getGjjCompanyRule());

                    if (companyHrmProgrammeRoundUp.getType() == 1) {
                        gJJGSHJ = gJJGSHJ.setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                    } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                        gJJGSHJ = gJJGSHJ.setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);
                    } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                        gJJGSHJ = gJJGSHJ.setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);
                    }
                    if (employeeHrmProgrammeRoundUp.getType() == 1) {
                        gJJGRHj = gJJGRHj.setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                    } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                        gJJGRHj = gJJGRHj.setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);

                    } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                        gJJGRHj = gJJGRHj.setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);
                    }
//                    result.put("accumulation", resultList);
//                    result.put("accumulationGRHj", gJJGRHj + "");
//                    result.put("accumulationGSHJ", gJJGSHJ + "");
//                    result.put("accumulationHJ", gJJGRHj.add(gJJGSHJ) + "");
//                    hrmProgrammeEmployeeRecordA.setAccumulationAmount(gJJGSHJ);
//                    hrmProgrammeEmployeeRecordA.setEmployeeAccumulationAmount(gJJGRHj);
                } else {
                    List<HrmProgrammeInfo> hrmProgrammeInfoSbs = hrmProgrammeInfoMapper.selectList(new LambdaQueryWrapper<HrmProgrammeInfo>() {{
                        eq(HrmProgrammeInfo::getProgrammeId, hrmAccumulation.getId());
                        in(HrmProgrammeInfo::getInsuranceId, hrmAccumulationInsurances.stream().map(HrmProgrammeInsurance::getId).collect(Collectors.toList()));
                    }});
                    List<JSONObject> resultList = new ArrayList<>();
                    BigDecimal gJJGRHj = new BigDecimal(0);
                    BigDecimal gJJGSHJ = new BigDecimal(0);
                    for (HrmProgrammeInfo hrmProgrammeInfo : hrmProgrammeInfoSbs) {
                        Integer limit = 0;
                        BigDecimal employeeJNAmount = new BigDecimal(0);
                        BigDecimal companyAmount = new BigDecimal(0);
                        HrmProgrammeInsurance employeeHrmProgrammeInsurance = hrmProgrammeInsuranceMapper.selectById(hrmProgrammeInfo.getInsuranceId());
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("name", employeeHrmProgrammeInsurance.getName());
                        ArrayList arrayList = JSONObject.parseObject(accumulationEmployee.getAccumulationLimitStr(), ArrayList.class);
                        for (Object j : arrayList) {
                            JSONObject jsonObject1 = JSONObject.parseObject(JSONObject.toJSONString(j));
                            if (jsonObject1.getInteger("insuranceId") == employeeHrmProgrammeInsurance.getId()) {
                                limit = jsonObject1.getInteger("amount");
                                break;
                            }
                        }
                        HrmProgrammeRoundUp employeeHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmProgrammeInfo.getIndividualRule());
                        HrmProgrammeRoundUp companyHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmProgrammeInfo.getCompanyRule());
                        JSONObject putResult = new JSONObject();
                        if (employeeHrmProgrammeRoundUp.getType() == 1) {
                            employeeJNAmount = new BigDecimal(limit).multiply(new BigDecimal(hrmProgrammeInfo.getIndividualProportion()).divide(new BigDecimal(100))).setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                        } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                            employeeJNAmount = new BigDecimal(limit).multiply(new BigDecimal(hrmProgrammeInfo.getIndividualProportion()).divide(new BigDecimal(100))).setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);

                        } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                            employeeJNAmount = new BigDecimal(limit).multiply(new BigDecimal(hrmProgrammeInfo.getIndividualProportion()).divide(new BigDecimal(100))).setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);

                        }
                        gJJGRHj = gJJGRHj.add(employeeJNAmount);
                        putResult.put("amount", employeeJNAmount + "");
                        putResult.put("uuId", UUID.randomUUID());
                        putResult.put("name", employeeHrmProgrammeInsurance.getName());
                        putResult.put("limit", new BigDecimal(limit) + "");
                        putResult.put("individualProportion", hrmProgrammeInfo.getIndividualProportion() + "%");
                        putResult.put("zName", "个人");
                        jsonObject.put("individualData", putResult);
                        resultList.add(putResult);
                        if (companyHrmProgrammeRoundUp.getType() == 1) {
                            companyAmount = new BigDecimal(limit).multiply(new BigDecimal(hrmProgrammeInfo.getCompanyProportion()).divide(new BigDecimal(100))).setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                        } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                            companyAmount = new BigDecimal(limit).multiply(new BigDecimal(hrmProgrammeInfo.getCompanyProportion()).divide(new BigDecimal(100))).setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);
                        } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                            companyAmount = new BigDecimal(limit).multiply(new BigDecimal(hrmProgrammeInfo.getCompanyProportion()).divide(new BigDecimal(100))).setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);
                        }
                        gJJGSHJ = gJJGSHJ.add(companyAmount);
                        putResult = new JSONObject();
                        putResult.put("uuId", UUID.randomUUID());
                        putResult.put("name", employeeHrmProgrammeInsurance.getName());
                        putResult.put("amount", companyAmount + "");
                        putResult.put("limit", new BigDecimal(limit) + "");
                        putResult.put("individualProportion", hrmProgrammeInfo.getCompanyProportion() + "%");
                        putResult.put("zName", "公司");
                        jsonObject.put("companyData", putResult);
                        jsonObject.put("JNZE", companyAmount.add(employeeJNAmount) + "");
                        resultList.add(putResult);
                    }
                    HrmProgrammeRoundUp employeeHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmAccumulation.getGjjIndividualRule());
                    HrmProgrammeRoundUp companyHrmProgrammeRoundUp = hrmProgrammeRoundUpMapper.selectById(hrmAccumulation.getGjjCompanyRule());

                    if (companyHrmProgrammeRoundUp.getType() == 1) {
                        gJJGSHJ = gJJGSHJ.setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                    } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                        gJJGSHJ = gJJGSHJ.setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);
                    } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                        gJJGSHJ = gJJGSHJ.setScale(companyHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);
                    }

                    if (employeeHrmProgrammeRoundUp.getType() == 1) {
                        gJJGRHj = gJJGRHj.setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_HALF_UP);
                    } else if (employeeHrmProgrammeRoundUp.getType() == 2) {
                        gJJGRHj = gJJGRHj.setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_DOWN);

                    } else if (employeeHrmProgrammeRoundUp.getType() == 3) {
                        gJJGRHj = gJJGRHj.setScale(employeeHrmProgrammeRoundUp.getRoundUp(), BigDecimal.ROUND_FLOOR);

                    }
//                    result.put("accumulation", resultList);
//                    result.put("accumulationGRHj", gJJGRHj + "");
//                    result.put("accumulationGSHJ", gJJGSHJ + "");
//                    result.put("accumulationHJ", gJJGRHj.add(gJJGSHJ) + "");
//                    hrmProgrammeEmployeeRecordA.setAccumulationAmount(gJJGSHJ);
//                    hrmProgrammeEmployeeRecordA.setEmployeeAccumulationAmount(gJJGRHj);
//                    hrmProgrammeEmployeeRecordMapper.updateById(hrmProgrammeEmployeeRecordA);

                    result.put("gJJGRHj", gJJGRHj);
                }
            } else {
//                hrmProgrammeEmployeeRecordA.setAccumulationAmount(new BigDecimal(0));
//                hrmProgrammeEmployeeRecordA.setEmployeeAccumulationAmount(new BigDecimal(0));
//                result.put("accumulation", new ArrayList<>());
//                result.put("accumulationGRHj", 0);
//                result.put("accumulationGSHJ", 0);
//                result.put("accumulationHJ", 0);
//                hrmProgrammeEmployeeRecordMapper.updateById(hrmProgrammeEmployeeRecordA);
                result.put("gJJGRHj", BigDecimal.ZERO);
            }
        }
//        hrmProgrammeEmployeeRecord.setHjAmount(hrmProgrammeEmployeeRecord.getProgrammeAmount().add(hrmProgrammeEmployeeRecord.getAccumulationAmount()).add(hrmProgrammeEmployeeRecord.getEmployeeProgrammeAmount()).add(hrmProgrammeEmployeeRecord.getEmployeeAccumulationAmount()));
//
//        result.put("HjAmount", hrmProgrammeEmployeeRecord.getHjAmount());
//        hrmProgrammeEmployeeRecord.setDataStr(JSONObject.toJSONString(result));

        return result;
    }

    @Override
    public String getRemark(Integer id) {
        HrmProgrammeEmployeeRecord hrmProgrammeEmployeeRecord = hrmProgrammeEmployeeRecordMapper.selectById(id);
        return hrmProgrammeEmployeeRecord.getRemark();
    }

    @Override
    public Integer updateRemark(Integer id, String remark) {

        HrmProgrammeEmployeeRecord hrmProgrammeEmployeeRecord = hrmProgrammeEmployeeRecordMapper.selectById(id);
        hrmProgrammeEmployeeRecord.setRemark(remark);

        hrmProgrammeEmployeeRecordMapper.updateById(hrmProgrammeEmployeeRecord);


        return 0;
    }


    @Override
    public void updateEmployeeZdMX(JSONObject jsonObject) {
        Integer employeeId = jsonObject.getInteger("employeeId");
        String month = jsonObject.getString("month");
        JSONObject data = jsonObject.getJSONObject("data");
        HrmProgrammeEmployeeRecord hrmProgrammeEmployeeRecord = hrmProgrammeEmployeeRecordMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployeeRecord>() {{
            eq(HrmProgrammeEmployeeRecord::getEmployeeId, employeeId);
            eq(HrmProgrammeEmployeeRecord::getProgrammeStartTime, month);
            last("limit 1");
        }});
        String jsonString = JSONObject.toJSONString(data);
        hrmProgrammeEmployeeRecord.setDataStr(jsonString);
        hrmProgrammeEmployeeRecord.setIsUpdate(2);
        hrmProgrammeEmployeeRecordMapper.updateById(hrmProgrammeEmployeeRecord);
    }

    public JSONObject generateRecord(Integer employeeId, String month) {
        Integer pEmployeeId = null;
        Integer aEmployeeId = null;
        HrmProgrammeEmployee programmeEmployee = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getUserId, employeeId);
            le(HrmProgrammeEmployee::getProgrammeStartTime, month);
            eq(HrmProgrammeEmployee::getProgrammeState, 1);
            eq(HrmProgrammeEmployee::getIsProgramme, 1);
            orderByDesc(HrmProgrammeEmployee::getProgrammeStartTime);
            last("limit 1");
        }});
        HrmProgrammeEmployee accumulationEmployee = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getUserId, employeeId);
            le(HrmProgrammeEmployee::getAccumulationStartTime, month);
            eq(HrmProgrammeEmployee::getProgrammeState, 1);
            eq(HrmProgrammeEmployee::getIsAccumulation, 1);
            orderByDesc(HrmProgrammeEmployee::getAccumulationStartTime);
            last("limit 1");
        }});
        HrmProgrammeEmployee programmeEmployeeStop = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getUserId, employeeId);
            le(HrmProgrammeEmployee::getProgrammeStartTime, month);
            eq(HrmProgrammeEmployee::getProgrammeState, 2);
            eq(HrmProgrammeEmployee::getIsProgramme, 1);
            orderByDesc(HrmProgrammeEmployee::getProgrammeStartTime);
            last("limit 1");
        }});
        HrmProgrammeEmployee accumulationEmployeeStop = hrmProgrammeEmployeeMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployee>() {{
            eq(HrmProgrammeEmployee::getUserId, employeeId);
            le(HrmProgrammeEmployee::getAccumulationStartTime, month);
            eq(HrmProgrammeEmployee::getProgrammeState, 2);
            eq(HrmProgrammeEmployee::getIsAccumulation, 1);
            orderByDesc(HrmProgrammeEmployee::getAccumulationStartTime);
            last("limit 1");
        }});

        HrmProgrammeEmployeeRecord programmeEmployeeRecord = new HrmProgrammeEmployeeRecord();
        if (ObjectUtils.isEmpty(programmeEmployeeStop) && ObjectUtils.isEmpty(accumulationEmployeeStop)) {
            JSONObject result = new JSONObject();
            //未停止且有当前缴纳社保费
            if (ObjectUtils.isEmpty(programmeEmployeeStop)) {
                if (!ObjectUtils.isEmpty(programmeEmployee)) {

                    programmeEmployeeRecord.setCompanyId(programmeEmployee.getCompanyId());
                    programmeEmployeeRecord.setAccountId(programmeEmployee.getAccountId());
                    programmeEmployeeRecord.setIsProgramme(1);
                    programmeEmployeeRecord.setProgrammeEmployeeId(programmeEmployee.getId());
                    programmeEmployeeRecord.setEmployeeId(employeeId);
                    programmeEmployeeRecord.setProgrammeStartTime(month);
                    programmeEmployeeRecord.setProgrammeAmount(programmeEmployee.getProgrammeLimit());
                    pEmployeeId = programmeEmployee.getId();
                }
                if (ObjectUtils.isEmpty(accumulationEmployeeStop)) {
                    if (!ObjectUtils.isEmpty(accumulationEmployee)) {
                        programmeEmployeeRecord.setCompanyId(accumulationEmployee.getCompanyId());
                        programmeEmployeeRecord.setAccountId(accumulationEmployee.getAccountId());
                        programmeEmployeeRecord.setIsAccumulation(1);
                        programmeEmployeeRecord.setAccumulationEmployeeId(accumulationEmployee.getId());
                        programmeEmployeeRecord.setEmployeeId(employeeId);
                        programmeEmployeeRecord.setAccumulationStartTime(month);
                        programmeEmployeeRecord.setAccumulationAmount(accumulationEmployee.getProgrammeLimit());
                        aEmployeeId = accumulationEmployee.getId();
                    }
                }
            }
        }
        if (!ObjectUtils.isEmpty(programmeEmployeeRecord)) {
            Long Pl = hrmProgrammeEmployeeRecordMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployeeRecord>() {{
                eq(HrmProgrammeEmployeeRecord::getEmployeeId, employeeId);
                eq(HrmProgrammeEmployeeRecord::getIsProgramme, programmeEmployeeRecord.getIsProgramme());
                eq(HrmProgrammeEmployeeRecord::getProgrammeStartTime, month);
            }});
            Long Al = hrmProgrammeEmployeeRecordMapper.selectCount(new LambdaQueryWrapper<HrmProgrammeEmployeeRecord>() {{
                eq(HrmProgrammeEmployeeRecord::getEmployeeId, employeeId);
                eq(HrmProgrammeEmployeeRecord::getIsAccumulation, programmeEmployeeRecord.getIsAccumulation());
                eq(HrmProgrammeEmployeeRecord::getProgrammeStartTime, month);
            }});
            if (Pl > 0 || Al > 0) {
                HrmProgrammeEmployeeRecord hrmProgrammeEmployeeRecord = hrmProgrammeEmployeeRecordMapper.selectOne(new LambdaQueryWrapper<HrmProgrammeEmployeeRecord>() {{
                    eq(HrmProgrammeEmployeeRecord::getEmployeeId, employeeId);
                    eq(HrmProgrammeEmployeeRecord::getProgrammeStartTime, month);
                }});
                programmeEmployeeRecord.setId(hrmProgrammeEmployeeRecord.getId());
                programmeEmployeeRecord.setIsUpdate(1);
                hrmProgrammeEmployeeRecordMapper.updateById(programmeEmployeeRecord);
                JSONObject jsonObject = generateEmployeeIdZdMX(employeeId, month, pEmployeeId, aEmployeeId);
                System.err.println(jsonObject + "jsonObject");
                return jsonObject;
            } else {
                programmeEmployeeRecord.setIsUpdate(1);
                hrmProgrammeEmployeeRecordMapper.insert(programmeEmployeeRecord);
                JSONObject jsonObject = generateEmployeeIdZdMX(employeeId, month, pEmployeeId, aEmployeeId);
                System.err.println(jsonObject + "jsonObject");
                return jsonObject;
            }
        }
        JSONObject result = new JSONObject();
        result.put("gJJGRHj", BigDecimal.ZERO);
        result.put("sBGRHj", BigDecimal.ZERO);
        return result;
    }

    public void importP(ProgrammeEmployeeSaveBo programmeEmployeeSaveBo) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        HrmProgrammeEmployee programmeEmployee = new HrmProgrammeEmployee();
        Integer employeeId = programmeEmployeeSaveBo.getEmployeeId();
        EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
        programmeEmployee.setProgrammeId(programmeEmployeeSaveBo.getProgrammeId());
        programmeEmployee.setUserId(employeeDO.getId());
        programmeEmployee.setUserName(employeeDO.getName());
        programmeEmployee.setUserType(employeeDO.getEmploymentType());
        programmeEmployee.setIdNumber(employeeDO.getIdNumber());
        programmeEmployee.setPhone(employeeDO.getMobile());
        programmeEmployee.setDepartmentId(employeeDO.getDeptId());
        CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
        if (!ObjectUtils.isEmpty(dept.getData())) {
            programmeEmployee.setDepartmentName(dept.getCheckedData().getName());
        }
        programmeEmployee.setProgrammeState(employeeDO.getState());
        programmeEmployee.setCompanyId(programmeEmployeeSaveBo.getCompanyId());
        programmeEmployee.setIsProgramme(programmeEmployeeSaveBo.getIsProgramme());
        programmeEmployee.setProgrammeStartTime(programmeEmployeeSaveBo.getProgrammeStartTime());
        programmeEmployee.setProgrammeLimit(programmeEmployeeSaveBo.getProgrammeLimit());
        programmeEmployee.setIsAccumulation(programmeEmployeeSaveBo.getIsAccumulation());
        programmeEmployee.setAccumulationStartTime(programmeEmployeeSaveBo.getAccumulationStartTime());
        programmeEmployee.setAccumulationLimit(programmeEmployeeSaveBo.getAccumulationLimit());
        hrmProgrammeEmployeeMapper.insert(programmeEmployee);
    }

    public static void main(String[] args) {
        System.err.println(new BigDecimal(800).compareTo(new BigDecimal(200)));
    }
}
